Created
September 30, 2014 23:58
-
-
Save briancarycom/dda639fdc5b44e322151 to your computer and use it in GitHub Desktop.
Tracking API cookbook
This file contains hidden or 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._pa = window._pa || {}; | |
// _pa.orderId = "myOrderId"; // OPTIONAL: attach unique conversion identifier to conversions | |
// _pa.revenue = "19.99"; // OPTIONAL: attach dynamic purchase values to conversions | |
// _pa.productId = "myProductId"; // OPTIONAL: Include product ID for use with dynamic ads | |
var pa = document.createElement('script'); pa.type = 'text/javascript'; pa.async = true; | |
pa.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + "//tag.perfectaudience.com/serve/5356d3fed85252febe000003.js"; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pa, s); | |
})(); | |
var output = { | |
log: function(txt) { | |
var el = document.getElementById("output"); | |
el.innerHTML = el.innerHTML + txt + "<br>"; | |
} | |
}; | |
// Example 1 - Visitor on page for 3 seconds | |
window.onload = function() | |
{ | |
var example1 = { | |
site_visit: function () { | |
setTimeout(function(){ | |
_pq.push(["track"]); | |
// Below is additional | |
output.log("<hr>Example 1 fired!"); | |
var el = document.getElementById("demo1"); | |
el.style.backgroundColor = "#33CC66"; | |
}, 3000); | |
} | |
}; | |
example1.site_visit(); | |
// Example 2 - Visitor hovered over element of class | |
var example2 = { | |
track_hover: function () { | |
_pq.push(["track"]); | |
// Below is additional | |
output.log("<hr>Example 2a fired!"); | |
var el = document.getElementById("demo2a"); | |
el.style.backgroundColor = "#33CC66"; | |
} | |
}; | |
var klass = "demo2a"; | |
var el = document.getElementsByClassName(klass)[0]; | |
el.onmouseenter = function() { example2.track_hover(); }; | |
// Example 2b - Visitor hovered over element of ID | |
var example2b = { | |
track_hover: function () { | |
_pq.push(["track"]); | |
// Below is additional | |
output.log("<hr>Example 2b fired!"); | |
var el = document.getElementById("demo2b"); | |
el.style.backgroundColor = "#33CC66"; | |
} | |
}; | |
var klass = "demo2b"; | |
var el_b = document.getElementsByClassName(klass)[0]; | |
el_b.onmouseenter = function() { example2b.track_hover(); }; | |
// Example 3a - clicked element of ID | |
var example3 = { | |
track_click: function() { | |
_pq.push(["track"]); | |
// Below is additional | |
output.log("<hr>Example 3a fired!"); | |
var el = document.getElementById("demo3a"); | |
el.style.backgroundColor = "#33CC66"; | |
} | |
}; | |
var id = "demo3a"; | |
var el_3a = document.getElementById(id); | |
el_3a.onclick = function() { example3.track_click(); } | |
// Example 3b - clicked element of ID | |
var example3b = { | |
track_click: function() { | |
_pq.push(["track"]); | |
// Below is additional | |
output.log("<hr>Example 3b fired!"); | |
var el = document.getElementById("demo3b"); | |
el.style.backgroundColor = "#33CC66"; | |
} | |
}; | |
var id = "demo3b"; | |
var el_3b = document.getElementById(id); | |
el_3b.onclick = function() { example3b.track_click(); } | |
// Example 4 - user came from Google/Facebook/Twitter | |
var example4 = { | |
initialize: function() { | |
if (document.referrer != "") { | |
var referringURL = document.referrer; | |
if (ref.match(/^https?:\/\/([^\/]+\.)?google\.com(\/|$)/i)) { | |
_pq.push(["track", { referer: 'google'}]); | |
output.log("<hr>Example 4 fired - user came from Google"); | |
} | |
if (ref.match(/^https?:\/\/([^\/]+\.)?facebook\.com(\/|$)/i)) { | |
pq.push(["track", { referer: 'facebook'}]); | |
output.log("<hr>Example 4 fired - user came from Facebook"); | |
} | |
if (ref.match(/^https?:\/\/([^\/]+\.)?twitter\.com(\/|$)/i)) { | |
_pq.push(["track", { referer: 'twitter'}]); | |
output.log("<hr>Example 4 fired - user came from Twitter"); | |
} | |
// catch all | |
_pq.push(["track", { referer: referringURL}]); | |
output.log("<hr>Example 4 fired - user came from " + referringURL); | |
} else { | |
output.log("<hr>Example 4 - no referrer"); | |
} | |
} | |
}; | |
example4.initialize(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment