Skip to content

Instantly share code, notes, and snippets.

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-TGVFDQ"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-TGVFDQ');</script>
<!-- End Google Tag Manager -->
@paulmkoch
paulmkoch / Viewport Dimensions
Created May 15, 2014 16:31
Viewport Dimensions
<script>
if (window.$) {
var width = $(window).width();
var height = $(window).height();
ga('send', 'event', 'Viewport', 'Size', width + 'x' + height, 0, {'nonInteraction': 1});
}
</script>
@paulmkoch
paulmkoch / Element Has an Existing Data Attribute
Created May 15, 2014 16:51
Element Has an Existing Data Attribute
// This macro looks for elements with data-track-event.
// To look for other attributes, replace "data-track-event" below.
// Note that it applies only to a specific element, and doesn't appropriately bubble.
function() {
var tracking = {{element}}.getAttribute('data-track-event');
if (tracking) {
return true;
} else {
return false;
@paulmkoch
paulmkoch / Event Category, Action, or Label of Existing Data Attribute
Created May 15, 2014 16:52
Event Category, Action, or Label of Existing Data Attribute
// replace the attribute with the appropriate data attribute
// if the attribute is divided with something other than commas, change tracking.split
// change the number to 0, 1, or 2, depending on whether you want the category, action, or label
function() {
var tracking = {{element}}.getAttribute('data-track-event');
tracking = tracking.split(',');
@paulmkoch
paulmkoch / Element URL Matches Current URL with Hash
Created May 15, 2014 16:53
Element URL Matches Current URL with Hash
function(){
if ( {{element url}}.indexOf({{url}} + '#') === 0 ) {
return true;
}
return false;
}
@paulmkoch
paulmkoch / Pageview onhashchange
Created May 15, 2014 17:04
Pageview onhashchange
<script>
var pageView = function() {
_gaq.push(['_trackPageview', {{url with hash}}]);
};
if (!('onhashchange' in window)) {
var oldHref = location.href;
setInterval(function() {
var newHref = location.href;
@paulmkoch
paulmkoch / URL with hash
Created May 15, 2014 17:05
URL with hash
function() {
var urlWithHash = location.pathname + location.search + location.hash;
return urlWithHash;
}
@paulmkoch
paulmkoch / Get Values from Data Attribute
Created September 11, 2014 22:16
Get Values from Data Attribute
// Returns: Values of data attribute
// Replace 'data-track-gtm' with the data attribute you're seeking
function() {
var isSet = function(val) {
return val !== null && val !== '';
};
var el = {{element}};
var val = el.getAttribute('data-track-gtm');
@paulmkoch
paulmkoch / Event Category from Data Attribute
Created September 11, 2014 22:22
Event Category from Data Attribute
// This macro looks for data attributes whose values are separated by a pipe ('|'). Replace the pipe with the separator your data attribute uses, such as a comma.
function() {
tracking = {{Get Values from Data Attribute}};
tracking = tracking.split('|');
return tracking[0];
}
@paulmkoch
paulmkoch / Event Action from Data Attribute
Created September 11, 2014 22:23
Event Action from Data Attribute
// This macro looks for data attributes whose values are separated by a pipe ('|'). Replace the pipe with the separator your data attribute uses, such as a comma.
function() {
tracking = {{Get Values from Data Attribute}};
tracking = tracking.split('|');
return tracking[1];
}