GA Measurement Link Generator

by Vladimir

HTML

<h1>Measurement protocol GA's link generator</h1>
<p>Sending both (event &amp; pageview) requests</p>
<b>TID:</b> <input id="tid" value="UA-32725515-4" /><br />
<b>CID:</b> <input id="cid" value="00000000-BFDA-43D3-9829-D31528543588" /><br />
<b>Type:</b> both<br />
<b>Category:</b> <input id="ec" value="SB_client" /><br />
<b>Action:</b> <input id="ea" value="Install" /><br />
<b>Label:</b> <input id="el" value="Error .Net Install EROORCODE" /><br />
<b>Value:</b> 1<br />
<b>Pageview:</b> /<input id="dp" value="sb_client_install_error_dot_net_install_ERRORCODE" /><br />
<input type="button" value="Generate" id="gen" />
<div></div>
<img id="event" />
<img id="pageview" />

CSS

body {
    font-family: Verdana;
}
input {
    width: 300px;
}
.grey {
    background: #DDD;
}

JavaScript

var baseUrl = "http://www.google-analytics.com/collect?v=1&";
var linkEvent, linkEPageview;
$('#gen').click(function() {
	var d = new Date(),
		dayTimestamp = parseInt((d - (new Date(d.getFullYear(), d.getMonth(), d.getDate())))/1000);

    linkEvent = baseUrl + 'tid=' + $('#tid').val() +
        '&cid=' + $('#cid').val() +
        '&t=event' + 
        '&ec=' + encodeURI($('#ec').val()) + 
        '&ea=' + encodeURI($('#ea').val()) +
        '&el=' + encodeURI($('#el').val()) +
        '&ev=1' + 
        '&cd1=' + $('#cid').val() +
        '&cd8=' + encodeURI($('#ea').val() + ' ' + $('#ec').val()) +
        '&cd9=' + encodeURI($('#el').val()) +
        '&cm11=' + dayTimestamp;

    linkEPageview = baseUrl + 'tid=' + $('#tid').val() +
        '&cid=' + $('#cid').val() +
        '&t=pageview' + 
        '&dp=%2F' + $('#dp').val();

    $('div').html('<div class="grey">' + linkEvent + '</div><br />' + linkEPageview);
    $('#event').attr('src', linkEvent);
    $('#pageview').attr('src', linkEPageview);
});