Created
May 11, 2017 17:09
-
-
Save franklin-like-the-turtle/74fd0c4e35512488727b0e9b807e9577 to your computer and use it in GitHub Desktop.
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
<script> | |
(function() { | |
//set gaCrumb default | |
//create gaCrumb from breadcrumb | |
gaCrumb = '(not set)'; | |
var gaCrumbPrep = []; | |
if ($('.breadcrumb').length > 0) { | |
$('.breadcrumb a').each(function() { | |
var crumbText = $(this).text(); | |
if (crumbText.toLowerCase() !== 'home') { | |
gaCrumbPrep.push(crumbText); | |
} | |
}); | |
var gaTitle = document.title.replace(/[^a-zA-Z\d\s]/g, '').toLowerCase(); | |
gaCrumbPrep = '/' + gaCrumbPrep.join('/').replace(/[^a-zA-Z\d\s\/]/g, '').toLowerCase() + '/' + gaTitle; | |
gaCrumb = gaCrumbPrep.replace(/ +/g, '-') | |
} else { | |
gaCrumb = '/'; | |
} | |
if (window.location.hash.indexOf('#') !== -1) { | |
gaCrumb = gaCrumb + '/' + window.location.hash; | |
} | |
if (typeof staticGACrumb !== 'undefined') { | |
dataLayer.push({ | |
'event': 'gaCrumb', | |
'breadcrumb': staticGACrumb | |
}); | |
} else if (gaCrumb.indexOf('/') > -1) { | |
dataLayer.push({ | |
'event': 'gaCrumb', | |
'breadcrumb': gaCrumb | |
}); | |
} | |
//initial virtualPageView for Special Content | |
if (typeof staticGACrumb !== 'undefined') { | |
var virtualPageView = { | |
'event': 'virtualPageView' | |
} | |
virtualPageView.virtualPageURL = staticGACrumb + '/' + window.location.hash; | |
virtualPageView.virtualPageTitle = document.title; | |
//Push to GTM dataLayer | |
dataLayer.push(virtualPageView) | |
} | |
//define onhashchange event for virtualPageViews | |
function locationHashChanged() { | |
if (location.hash.indexOf('#/') > -1) { | |
//Define event 'virtualPageView' | |
var virtualPageView = { | |
'event': 'virtualPageView' | |
} | |
//Apply event params | |
//Override GA Crumb with staticGACrumb if exists in source | |
if (typeof staticGACrumb !== 'undefined') { | |
var virtualEventCrumb = staticGACrumb | |
console.log('staticGACrumb', staticGACrumb, virtualEventCrumb) | |
} else { | |
virtualEventCrumb = gaCrumb | |
} | |
virtualPageView.virtualPageURL = virtualEventCrumb + '/' + window.location.hash; | |
virtualPageView.virtualPageTitle = document.title; | |
//Push to GTM dataLayer | |
dataLayer.push(virtualPageView) | |
} | |
} | |
window.onhashchange = locationHashChanged; | |
//add data-headline attrs to RFB slides | |
$('#rotating-feature-block .item').each(function() { | |
console.log('found rfb item') | |
var headline = $(this).find('h3').text().replace(/\s{2,}/g, ''); | |
$(this).find('.more').attr('data-headline', headline) | |
$(this).find('h3 a').attr('data-headline', headline) | |
$(this).find('a:has(img)').attr('data-headline', headline) | |
}); | |
}) | |
// PRINT INTENT | |
(function() { | |
var printType = 'other'; | |
var runOnce; | |
var afterPrint = function() { | |
if (!runOnce) { // Because of Chrome we can only allow the code to run once. | |
runOnce = true; | |
dataLayer.push({ | |
'event': 'virtualEvent', | |
'eventCategory': 'print-intent', | |
'eventAction': printType, | |
'eventLabel': gaCrumb | |
}); // Send Print Event to GTM | |
}; | |
}; | |
if (window.matchMedia) { // Track printing from browsers using the Webkit engine | |
var mediaQueryList = window.matchMedia('print'); | |
mediaQueryList.addListener(function(mql) { | |
if (!mql.matches) { | |
afterPrint(); | |
} | |
}); | |
} | |
window.onafterprint = afterPrint; // Internet Explorer | |
$(document).keydown(function(allBrowsers) { // Track printing using Ctrl/Cmd+P. | |
if (allBrowsers.keyCode == 80 && (allBrowsers.ctrlKey || allBrowsers.metaKey)) { | |
printType = 'keyboard-shortcut'; | |
if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. | |
afterPrint(); | |
} | |
} | |
}); | |
// Print activated using window.print() ex. from a button. | |
$('#addthis-print').on('click', function() { | |
printType = 'print-button'; | |
if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. | |
afterPrint(); | |
} | |
//window.print(); // If window.print() is activated in another script, you can remove it from this tracking script. | |
}); | |
}()); | |
//YOUTUBE: Add videos loaded dynamically in fancybox ".popup-youtube" | |
$('.popup-youtube').fancybox({ | |
afterShow: triggerYoutubeVideoAdded | |
}); | |
function triggerYoutubeVideoAdded() { | |
$(window).trigger('youtubeVideoAdded') | |
} | |
//TELEPHONE LINK CLICKS | |
$('a[href*="tel:"]').on('click', function() { | |
var phoneNumber = $(this).attr('href').replace('tel:', ''); | |
var currentPage = window.location.pathname; | |
reportVirtualEvent('Telephone Clicks', currentPage, phoneNumber); | |
}); | |
//SAFE CONSOLE LOGGING in IE | |
(function(con) { | |
'use strict'; | |
var prop, method; | |
var empty = {}; | |
var dummy = function() {}; | |
var properties = 'memory'.split(','); | |
var methods = ('assert,clear,count,debug,dir,dirxml,error,exception,group,' + | |
'groupCollapsed,groupEnd,info,log,markTimeline,profile,profiles,profileEnd,' + | |
'show,table,time,timeEnd,timeline,timelineEnd,timeStamp,trace,warn').split(','); | |
while (prop = properties.pop()) con[prop] = con[prop] || empty; | |
while (method = methods.pop()) con[method] = con[method] || dummy; | |
})(this.console = this.console || {}); | |
//REPORT VIRTUAL EVENT | |
function reportVirtualEvent(category, action, label) { | |
if (typeof(dataLayer) != "undefined") { | |
var virtualEvent = { | |
'event': 'virtualEvent' | |
} | |
if(category){virtualEvent.eventCategory = category;} | |
if(action){virtualEvent.eventAction = action;} | |
if(label){virtualEvent.eventLabel = label;} | |
dataLayer.push(virtualEvent) | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment