Last active
December 21, 2015 20:59
-
-
Save tuupola/6365648 to your computer and use it in GitHub Desktop.
Example on how to do Google Event tracking.
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
<a data-category="Links" data-action="Leasing calculator" data-label="Avensis" href="http://aabw.toyota.ee/leasing/et/avensis">Avensis</a> | |
<a data-category="Links" data-action="Test drive" data-label="Aygo" href="http://aabw.toyota.ee/testdrive/et#aygo">Aygo</a> | |
<!-- Insurance does not allow linking directly to a model but we can include intended model in data-label. --> | |
<a data-category="Links" data-action="Insurance calculator" data-label="Land Cruiser" href="http://aabw.toyota.ee/insurance/">Insurance</a> | |
<a data-category="Links" data-action="Fuel calculator" data-label="RAV4" href="http://kampaania.toyota.ee/fuel/et#auris">RAV4</a> | |
<a data-category="Links" data-action="Model page" data-label="GT86" href="http://www.toyota.ee/cars/new_cars/gt86/index.tmex">GT86</a> | |
<!-- PDFs and stuff should have different category. --> | |
<a data-category="Downloads" data-action="PDF" href="/downloads/yaris-something.pdf">Yaris something</a> | |
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
$("a[data-category]").on("click", function() { | |
var category = $(this).data("category"); | |
var action = $(this).data("action"); | |
var label = $(this).data("label"); | |
var target = $(this).attr("target"); | |
var url = $(this).attr("href"); | |
/* For downloads use the downloaded file as label. Keep the filenames pretty. */ | |
if ("Downloads" === category) { | |
label = url; | |
} | |
try { | |
_gaq.push(["_trackEvent", category, action, label]); | |
} catch(error) { | |
console.log(error); | |
} | |
if (target) { | |
window.open(url, target); | |
} else { | |
/* Give Google Analytics some time to record the event. */ | |
setTimeout(function() { | |
window.location = url; | |
}, 100); | |
} | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment