bheyde
5/19/2021 - 9:07 PM

lock Facebook Conversion Tracking

<JavaScriptResource_Add>
	<Code>facebook_global_tracking</Code>
	<Type>InlineScript</Type>
	<Global>Yes</Global>
	<Active>Yes</Active>
	<Attributes>
		<Attribute>
			<Name><![CDATA[async]]></Name>
			<Value><![CDATA[]]></Value>
		</Attribute>
	</Attributes>
	<Template><![CDATA[!function(f,b,e,v,n,t,s)
  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
  n.callMethod.apply(n,arguments):n.queue.push(arguments)};
  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
  n.queue=[];t=b.createElement(e);t.async=!0;
  t.src=v;s=b.getElementsByTagName(e)[0];
  s.parentNode.insertBefore(t,s)}(window, document,'script',
  'https://connect.facebook.net/en_US/fbevents.js');
  fbq('init', 'XXXXXXXXXXXXXX');
  fbq('track', 'PageView');]]></Template>
	<Notes>Original Template</Notes>
</JavaScriptResource_Add>

<JavaScriptResource_Add>
	<Code>facebook_conversion_tracking</Code>
	<Type>InlineScript</Type>
	<Global>No</Global>
	<Active>Yes</Active>
	<Attributes>
	</Attributes>
	<Template><![CDATA[<mvt:if expr="l.settings:page:code EQ 'INVC'">
<mvt:comment>
| Facebook Configuration. Get these values from Facebook Events Manager.
</mvt:comment>

<mvt:assign name="l.apiversion" 	value="'v10.0'" />
<mvt:assign name="l.pixelID" 		value="'XXXXXXXXXXXX'" />
<mvt:assign name="l.accesstoken" 	value="'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'" />

<mvt:comment>
| Example endpoint https://graph.facebook.com/{API_VERSION}/{PIXEL_ID}/events?access_token={TOKEN}
</mvt:comment>

<mvt:assign name="l.facebook:api_endpoint" value="'https://graph.facebook.com/' $ l.apiversion $ '/' $ l.pixelID $ '/events?access_token=' $ l.accesstoken" />


<mvt:assign name="l.user_agent" 	value="s.http_user_agent" />
<mvt:assign name="l.user_ip" 		value="s.remote_addr" />
<mvt:assign name="l.currency" 		value="'USD'" />

<mvt:assign name="l.success" value="crypto_sha256( tolower( l.settings:order:bill_email ), 'hex', l.facebook:email )" />


<mvt:capture variable = "l.json_data">
{ "data" : 
   [
       {
         "event_name": "Purchase",
         "event_time": <mvt:eval expr="encodejavascriptstring( s.dyn_time_t) "/>,
         "user_data": {
           "em": [ "<mvt:eval expr="encodejavascriptstring( l.facebook:email )" />"],
           "client_ip_address": "<mvt:eval expr="encodejavascriptstring( l.user_ip )" />",
           "client_user_agent": "<mvt:eval expr="encodejavascriptstring( l.user_agent )" />",
           "fbc": "<mvt:eval expr="encodejavascriptstring( g.request_cookies:_fbc)" />",
           "fbp": "<mvt:eval expr="encodejavascriptstring( g.request_cookies:_fbp)" />"
         },

	 "content_type" : "product",
         "contents": [
	   <mvt:foreach iterator="item" array="order:groups">
           {
             "id": "<mvt:eval expr="encodejavascriptstring( l.settings:item:code )" />",
             "quantity": <mvt:eval expr="encodejavascriptstring( l.settings:item:quantity )" />,
	     "item_price" : <mvt:eval expr="encodejavascriptstring( l.settings:item:price )" />,	
             "delivery_category": "home_delivery"
           }
	   
	   <mvt:if expr="POS1 LT miva_array_elements( l.settings:order:groups )">,</mvt:if>
          </mvt:foreach>
		
         ],
         "custom_data": {
           "currency": "<mvt:eval expr="encodejavascriptstring( l.currency )" />",
           "value": <mvt:eval expr="encodejavascriptstring( l.settings:order:total )" />,
	   "order_id": <mvt:eval expr="encodejavascriptstring( l.settings:order:id )" />
         },
         "event_source_url": "<mvt:eval expr="encodejavascriptstring( l.settings:urls:_self:auto )" />",
         "action_source": "website"
       }
     ]
}
</mvt:capture>

<mvt:comment>
| Send Data to Facebook. Response data is ignored.
</mvt:comment>

<mvt:capture variable = "l.response_raw">
	<miva MvCALL_Error = "nonfatal, nodisplay">
	<mvt:call 	METHOD = "'RAW'"
			CONTENT-TYPE = "'application/json'"
			ACTION = "l.facebook:api_endpoint"
			TIMEOUT = "5"
			FIELDS = "'json_data'" >

			<mvt:eval expr="s.callvalue" />
	</mvt:call>
	<miva MvCALL_Error = "fatal, display">
</mvt:capture>

</mvt:if>]]>
</Template>
	<Notes>Original Template</Notes>
</JavaScriptResource_Add>

<!-- Assign Resource to INVC-->
<JavaScriptResourceXPage_Assign code="facebook_conversion_tracking" page_code="INVC" />


<!-- Assign both resources to footer_js Resource Group-->
<JavaScriptResourceXGroup_Assign code="facebook_global_tracking" group_code="head_tag" />
<JavaScriptResourceXGroup_Assign code="facebook_conversion_tracking" group_code="head_tag" />