Last active
September 2, 2016 08:18
-
-
Save Themitchell/f408f0a95440131474fd32be5ad526f8 to your computer and use it in GitHub Desktop.
tracking.js
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
//modal overlay funcion | |
$(document).ready(function (){ | |
$('.utm-link').each(addUTMParams); | |
$('.utm-link').click(function (event) { | |
ga('send', 'event', $(event.target).attr('data-type'), 'click', getParameterByName('source') + ' / ' + getParameterByName('medium'), { | |
'transport': 'beacon' | |
}); | |
}); | |
}); // doc.ready | |
// Cal's custom UTL tracking | |
function addUTMParams (link) { | |
var $link = $(link); | |
var url = $link.attr('href') | |
var type = $link.attr('data-type'); | |
// Get URL utm parameters if present | |
var source = getParameterByName('utm_source'); | |
var medium = getParameterByName('utm_medium'); | |
var campaign = getParameterByName('utm_campaign'); | |
var term = getParameterByName('utm_term'); | |
var content = getParameterByName('utm_content'); | |
// If no source & medium, get the refferer and pass through | |
if (!source && !medium) { | |
source = 'referral'; | |
medium = document.referrer.split('/')[2]; | |
} | |
// If passing through to album preorder - use Songkick as content so we can claim as our traffic | |
if (type=='preorder') {content = 'songkick splash'}; | |
// Build query string | |
var queryString ="?from=splash"; | |
if (source) {queryString = queryString + '&utm_source=' + encodeURIComponent(source);} | |
if (medium) {queryString = queryString + '&utm_medium=' + encodeURIComponent(medium);} | |
if (campaign) {queryString = queryString + '&utm_campaign=' + encodeURIComponent(campaign);} | |
if (term) {queryString = queryString + '&utm_term=' + encodeURIComponent(term);} | |
if (content) {queryString = queryString + '&utm_content=' + encodeURIComponent(content);} | |
// Append parameters to URL | |
url += queryString | |
$link.attr('href', url); | |
console.log(url); | |
console.log(type); | |
} | |
function getParameterByName(name, url) { | |
if (!url) url = window.location.href; | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment