Last active
September 18, 2017 16:29
-
-
Save rdillmanCN/146ae766be86a4b5f0cb8b9cb54c9f9d to your computer and use it in GitHub Desktop.
Send a tracking event to facebook once all sparrow data is available
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
/** | |
* IFFE to kick it all off. | |
*/ | |
(function iffe() { | |
// setup the fbq facebook queue | |
!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'); | |
var killSwitch = Date.now() + 15000; // 15 seconds | |
/** | |
* Inject a facebook pixel in the page. | |
* | |
* @returns {undefined} nothing. | |
*/ | |
function injectPixel() { | |
var imgtag = document.createElement('img'); | |
imgtag.height = '1'; | |
imgtag.width = '1'; | |
imgtag.style = 'display:none;'; | |
imgtag.src = 'https://www.facebook.com/tr?id=228464857488266&ev=PageView'; | |
document.body.appendChild(imgtag); | |
} | |
/** | |
* Send a message to the console about missing data | |
* | |
* @param {string} message - The message | |
* @returns {undefined} nothing. | |
*/ | |
function warning(message) { | |
console.warn('Facebook Pixel had a problem.', message); | |
} | |
/** | |
* Updates the user or context 4d objects and send tracking to Facebook. | |
* | |
* | |
* @param {Object} fourd - The _4d object | |
* @returns {*} either a warning or nothing. | |
*/ | |
function fbtrack(fourd) { | |
var user = fourd && fourd.user; | |
var atr = user && user.atr; | |
var attribute = JSON.stringify(atr); | |
var cache = window.SparrowCache; | |
var track = cache && cache.pixels && cache.pixels[0] && cache.pixels[0].track; | |
var xid = user && user.xid; | |
// exit early if _4d.user.xid or context are missing | |
if (!fourd.context || !fourd.user.xid) { | |
warning('Missing the _4d.context object or _4d.user.xid.'); | |
return; | |
} | |
// fire a fbq xid event | |
window.fbq('trackCustom', 'xidTrack', { xid: xid }); | |
// exit early if _4d.user.atr is missing | |
if (!atr) { | |
warning('Missing the _4d.user.atr.'); | |
return; | |
} | |
// update _4d.user.atr.xid | |
atr.xid = xid; | |
// fire a fbq spire event | |
window.fbq('trackCustom', 'spire_nyr_rpt_high_prop', atr); | |
if (!track) { | |
warning('Missing the SparrowCache.pixels[0].track function.'); | |
return; | |
} | |
// fire a sparrow track event | |
track('citizenNet-4d-fbTest', 'fb-pixel-attrCalled', { attr: attribute }); | |
} | |
/** | |
* Recursively check for the _4d.user object | |
* | |
* @returns {*} call fbtrack or recursively call ourselves | |
*/ | |
function check4d() { | |
var fourd = window.top._4d; | |
if (fourd && fourd.user) { | |
return fbtrack(fourd); | |
} | |
// stop at 15 seconds | |
if (Date.now() >= killSwitch) { | |
warning('15 second timeout reached _4d.user not found.'); | |
return false; | |
} | |
window.setTimeout(function check4dTimer() { | |
return check4d(); | |
}, 1000); | |
} | |
injectPixel(); | |
window.fbq('init', '228464857488266'); | |
window.fbq('track', 'PageView'); | |
check4d(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment