Skip to content

Instantly share code, notes, and snippets.

@chrisle chrisle/gist:8588282

Created Jan 23, 2014
What would you like to do?
TLDR - Google Analytics
* The very simplest explanation of how GA works in JavaScript is that it's simply an array.
* The GA tag (not the new Universal tag) looks similar to this:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-y']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
* That puts an array in your page's context called "_gaq". From here,
* you interact with it like any array.
// Track a pageview
* You can place this inside functions. For example...
// Your HTML looks like this:
<button id="my-button">Click Me</button>
// Your JS looks like this:
var trackClickEventsAsVirtualPageView = function(pageview) {
_gaq.push(['_trackPageview', pageview]);
$('#my-button').on('click', function() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.