Skip to content

Instantly share code, notes, and snippets.

@edmeehan-tcg
Created December 12, 2017 19:41
Show Gist options
  • Save edmeehan-tcg/6be5779581b1c6f3707b3bd62f467272 to your computer and use it in GitHub Desktop.
Save edmeehan-tcg/6be5779581b1c6f3707b3bd62f467272 to your computer and use it in GitHub Desktop.
Facebook Pixel
(function (window, document) {
if (window.myfbq) return;
window.myfbq = (function () {
if (arguments.length > 0) {
var pixelId, trackType, contentObj;
if (typeof arguments[0] == 'string') pixelId = arguments[0];
if (typeof arguments[1] == 'string') trackType = arguments[1];
if (typeof arguments[2] == 'object') contentObj = arguments[2];
var params = [];
if (typeof pixelId === 'string' && pixelId.replace(/\s+/gi, '') != '' &&
typeof trackType === 'string' && trackType.replace(/\s+/gi, '')) {
params.push('id=' + encodeURIComponent(pixelId));
switch (trackType) {
case 'PageView':
case 'ViewContent':
case 'Search':
case 'AddToCart':
case 'InitiateCheckout':
case 'AddPaymentInfo':
case 'Lead':
case 'CompleteRegistration':
case 'Purchase':
case 'AddToWishlist':
params.push('ev=' + encodeURIComponent(trackType));
break;
default:
return;
}
params.push('dl=' + encodeURIComponent(document.location.href));
if (document.referrer) params.push('rl=' + encodeURIComponent(document.referrer));
params.push('if=false');
params.push('ts=' + new Date().getTime());
if (typeof contentObj == 'object') {
for (var u in contentObj) {
if (typeof contentObj[u] == 'object' && contentObj[u] instanceof Array) {
if (contentObj[u].length > 0) {
for (var y = 0; y < contentObj[u].length; y++) { contentObj[u][y] = (contentObj[u][y] + '').replace(/^\s+|\s+$/gi, '').replace(/\s+/gi, ' ').replace(/,/gi, '§'); }
params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u].join(',').replace(/^/gi, '[\'').replace(/$/gi, '\']').replace(/,/gi, '\',\'').replace(/§/gi, '\,')));
}
}
else if (typeof contentObj[u] == 'string')
params.push('cd[' + u + ']=' + encodeURIComponent(contentObj[u]));
}
}
params.push('v=' + encodeURIComponent('2.7.19'));
var imgId = new Date().getTime();
var img = document.createElement('img');
img.id = 'fb_' + imgId, img.src = 'https://www.facebook.com/tr/?' + params.join('&'), img.width = 1, img.height = 1, img.style = 'display:none;';
document.body.appendChild(img);
window.setTimeout(function () { var t = document.getElementById('fb_' + imgId); t.parentElement.removeChild(t); }, 1000);
}
}
});
})(window, document);
@edmeehan-tcg
Copy link
Author

edmeehan-tcg commented Dec 12, 2017

Does not use the fbq script, but rather fires off a img pixel for each method call, this way you can keep tracking of individual event/hits if needed.

use methods example:

myfbq("[your-pixel-id]", "PageView");
myfbq("[your-pixel-id]", "ViewContent");
myfbq("[your-pixel-id]", "ViewContent", { content_type: "product", content_ids: ['892185001003'] });

@brunomarks7
Copy link

thanks for share

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment