Created
December 12, 2017 19:41
-
-
Save edmeehan-tcg/6be5779581b1c6f3707b3bd62f467272 to your computer and use it in GitHub Desktop.
Facebook Pixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for share