Skip to content

Instantly share code, notes, and snippets.

@phillipadsmith
Created February 28, 2020 18:45
Show Gist options
  • Save phillipadsmith/c6a723fe422b706beee89ab11769cb7f to your computer and use it in GitHub Desktop.
Save phillipadsmith/c6a723fe422b706beee89ab11769cb7f to your computer and use it in GitHub Desktop.
abstract-tracking.js
var adCampaignPicoHandler = function(){
var REGISTERED_EVENT_CAPTURED = "ad-reg-event-captured";
var PAID_EVENT_CAPTURED = "ad-paid-event-captured";
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function fireTrackingEvents(){
fireFBevent(event);
fireGAevent(event);
//Other Tracking events can go here.
}
function fireGAevent(){
ga('send', 'event', event);
}
function fireFBevent(){
!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', '{your-pixel-id-goes-here}');
fbq('trackSingleCustom', '{your-pixel-id-goes-here}', event);
}
var element = document.querySelector('div.PicoSignal');
var event = null
var observer = new MutationObserver(function(mutations) {
var hasSentRegistered = getCookie(REGISTERED_EVENT_CAPTURED);
var hasSentPayment = getCookie(PAID_EVENT_CAPTURED);
mutations.forEach(function(mutation) {
if (mutation.type == "attributes" && mutation.attributeName == "data-pico-status") {
if(element.getAttribute("data-pico-status") == "registered" && !hasSentRegistered){
event = "Registered"
fireTrackingEvents(event);
setCookie(REGISTERED_EVENT_CAPTURED,1,3650)
}
else if(element.getAttribute("data-pico-status") == "paying" && !hasSentPayment){
event ="Paying"
fireTrackingEvents(event);
setCookie(PAID_EVENT_CAPTURED,1,3650)
}
}
});
});
observer.observe(element, {
attributes: true
});
}
window.addEventListener('pico.loaded', adCampaignPicoHandler)
adCampaignPicoHandler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment