secret
Created

  • Download Gist
gistfile1.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
<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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.