Google Analytics
Localhost google analytics tracking through Analytics Measurement Protocol.
by Alex Cowan
HTML
<!-- Google Analytics -->
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', 'UA-130183387-2, 'auto');
ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->
JavaScript
// URL of the POST request
var url = "//www.google-analytics.com/collect"
var site = window.location.hostname
//Get the current unix time in JS and assign it to the cid
var cid = Date.now();
// Parameters of the POST request notice the different UA
var data = 'v=1&tid=UA-46159137-6&cid=' + cid + '&t=event&ec=' + site + '&ea=click&el=button'
document.getElementById('click').addEventListener('click', function() {
console.log("sent", data);
var xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
xhr.onload = function(e) {
console.log('loaded', e);
};
xhr.send(data);
});