Skip to content

Instantly share code, notes, and snippets.

@NicoMiceli
Created December 4, 2013 21:24
Show Gist options
  • Save NicoMiceli/98a34bd23c5b3f187b09 to your computer and use it in GitHub Desktop.
Save NicoMiceli/98a34bd23c5b3f187b09 to your computer and use it in GitHub Desktop.
<html>
<head>
<script>
// Normal Universal Analytics code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
//The sites UA code which is different then the profile of the data you want to track
ga('create', 'UA-11111111-1', 'nicomiceli.com');
ga('send', 'pageview');
</script>
</head>
<body>
<h1>Click the button</h1>
<button id='button'>click me</button>
<script>
var site = window.location.hostname
var url = "http://www.google-analytics.com/collect"
//Get the current unix time in JS and assign it to the cid
var cid = (new Date).getTime();
// Parameters of the POST request notice the different UA
var data ="v=1&tid=UA-40202040-2&cid="+cid+"&t=event&ec="+site+"&ea=click&el=button"
//Event listener for clicks on the button
document.getElementById("button").addEventListener("click",
function () {
//function that loads after every click
var xhr = new XMLHttpRequest();
xhr.open("POST", url, false);
xhr.onload = console.log('loaded')
xhr.send(data)
// Uncomment the following line for debugging
//console.log("sent to " + data)
},
false
);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment