Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GTM Facebook Pixel Loader
<script>
(function(){
var FBDATA = {
sku: JSON.parse('{{CHECKOUT SKUS}}'), // cookie variable with array of SKUs (products on checkout page)
ids: {{A - EDIT THIS - FACEBOOK PIXEL ID LIST}}, // Variable with Pixel IDs
host: '{{Page Hostname}}'
};
for( var k in FBDATA.ids ) {
if( k !== 'all' ) {
if( FBDATA.host.indexOf( k ) === -1 ) {
continue;
}
}
if( FBDATA.ids[k].length < 1 ) {
continue;
}
for( var kk in FBDATA.ids[k] ) {
if( FBDATA.ids[k][kk].indexOf( ':' ) !== -1 ) {
if( FBDATA.ids[k][kk].split(':')[0] !== '{{UTM_SOURCE}}' && '{{UTM_SOURCE}}' !== 'faceboook' ) {
continue;
}
}
fbq( 'init', FBDATA.ids[k][kk] );
fbq( 'trackSingle', FBDATA.ids[k][kk], 'InitiateCheckout' );
fbq( 'trackSingle', FBDATA.ids[k][kk], 'AddToCart', {
content_type: 'product',
content_ids: FBDATA.sku
});
}
}
})();
</script>
<script>
(function(){
var FBDATA = {
total: '{{PURCHASE TOTAL}}', // conversion purchase total
currency: '{{CHECKOUT CURRENCY}}', // currency USD
sku: '{{PURCHASE SKU}}', // products SKU string
ids: {{A - EDIT THIS - FACEBOOK PIXEL ID LIST}}, // again static variable
host: '{{Page Hostname}}'
};
for( var k in FBDATA.ids ) {
if( k !== 'all' ) {
if( FBDATA.host.indexOf( k ) === -1 ) {
continue;
}
}
if( FBDATA.ids[k].length < 1 ) {
continue;
}
for( var kk in FBDATA.ids[k] ) {
if( FBDATA.ids[k][kk].indexOf( ':' ) !== -1 ) {
if( FBDATA.ids[k][kk].split(':')[0] !== '{{UTM_SOURCE}}' && '{{UTM_SOURCE}}' !== 'faceboook' ) {
continue;
}
}
fbq( 'init', FBDATA.ids[k][kk] );
fbq( 'trackSingle', FBDATA.ids[k][kk], 'Purchase', {
value: ((FBDATA.total!=='')?(FBDATA.total*1):100),
currency: ((FBDATA.currency!=='')?FBDATA.currency:'USD'),
content_type: 'product',
content_id: FBDATA.sku
});
}
}
})();
</script>
<script>
(function(){
var FBDATA = {
ids: {{A - EDIT THIS - FACEBOOK PIXEL ID LIST}},
host: '{{Page Hostname}}'
};
for( var k in FBDATA.ids ) {
if( k !== 'all' ) {
if( FBDATA.host.indexOf( k ) === -1 ) {
continue;
}
}
if( FBDATA.ids[k].length < 1 ) {
continue;
}
for( var kk in FBDATA.ids[k] ) {
if( FBDATA.ids[k][kk].indexOf( ':' ) !== -1 ) {
if( FBDATA.ids[k][kk].split(':')[0] !== '{{UTM_SOURCE}}' && '{{UTM_SOURCE}}' !== 'faceboook' ) {
continue;
}
}
fbq( 'init', FBDATA.ids[k][kk] );
fbq( 'trackSingle', FBDATA.ids[k][kk], 'PageView' );
}
}
})();
</script>
function(){
return {
'all': [ // global not domain specific
'123123123', // if no source is defined it defaults to facebook as utm_source
'by-utm-source-name:123123123' // utm-source:pixel-id or just pixel-id
],
'by-domain.com': [ // for if more than one domain is in play
'some-affiliates-pixel:123123123'
],
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.