Implementing Better Visitor Analytics
Pacific Northwest Drupal Summit 2018
trackingId: | {string} |
---|---|
cookieDomain: | {string} |
ga('create', 'UA-107047227-2', 'auto');
plugin: | {string} |
---|
ga('require', 'linker');
key: | {string} |
---|---|
value: | {string} |
ga('set', 'dimension1', 'article');
key: | {number} |
---|---|
value: | {number} |
ga('set', 'metric1', 2);
title: | {string} | optional |
---|---|---|
location: | {string} | optional |
page: | {string} | optional |
ga('send', 'pageview');
category: | {string} | |
---|---|---|
action: | {string} | |
label: | {string} | optional |
value: | {number} | optional |
ga('send', 'event', 'video', 'play', 'cats.mp4');
category: | {string} | |
---|---|---|
var: | {string} | |
value: | {number} | |
label: | {string} | optional |
ga('send', 'timing', 'resource', 'get', 100);
network: | {string} |
---|---|
action: | {string} |
target: | {string} |
ga('send', 'social', 'twitter', 'tweet', 'articles/give-it-a-go-and-grow-your-own-herbs');
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'dimension1', 'article');
ga('send', 'pageview');
jQuery('video').on('play', function () {
ga('send', 'event', 'video', 'play', 'cats.mp4');
});
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
var startTime = new Date();
jQuery.get('/resource')
.done(function () {
var endTime = new Date();
var duration = endTime.getTime() - startTime.getTime();
ga('send', 'timing', 'resource', 'get', duration);
});
ga('send', 'event', {
eventCategory: 'video',
eventAction: 'play',
eventLabel: 'cats.mp4',
});
jQuery('body').on('click', 'a', function (event) {
var href = $(this).attr('href');
if (
!href.match('^(\d+):\/\/')
||
href.match('^(https?):\/\/' + window.location.host)
) {
return;
}
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: href,
transport: 'beacon'
});
});
jQuery('body').on('click', 'a', function (event) {
var href = $(this).attr('href');
if (
!href.match('^(\d+):\/\/')
||
href.match('^(https?):\/\/' + window.location.host)
) {
return;
}
event.preventDefault();
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: href,
hitCallback: function() {
document.location = href;
}
});
});