Skip to content
Zwetan Kjukov edited this page Aug 23, 2015 · 9 revisions

The simplest possible Tracker

In the past, we had people complaining about the size of a library of 30KB (which is completely ridiculous IMHO), and so to prevent such complains or to give you an overview of the minimun needed to send data to Google Analytics servers, we will describe a step by step here.

The Measurement Protocol

As you can see in the Measurement Protocol Developer Guide, to send data to Google Analytics servers you need 3 things:

  • a client ID
  • a well formatted payload
  • an HTTP POST or GET

The Client ID

As explained in Required Values For All Hits, you need 4 required values each time you send a request, all the rest is optional, but if one of those 4 is missing your request will fail.

The protocol version is a piece of cake, just use v=1.

The Tracking ID is quite easy too, you get it from the Google Analytics panel, something following this format UA-XXXX-Y.

The Hit Type is one of the following strings: pageview, screenview, event, etc. (documented here Hit Type).

So, the real hard parameter to manage is the Client ID (again documented here Client ID).

The problem is not really to generate that client ID but to save it and reuse it.

If for example you generate a new Client ID for each requests, the Google Analytics servers will see a different user for each request, something you really don't want.

Another example would be tracking the same user from 2 different environments: on a web page the user would be tracked by analytics.js and within that page a SWF file using analytics.swc would track also that user.

In that case, if you use 2 different Client ID, you can not follow the flow of the user browsing.

At the opposite, if you re-use the same Client ID, you can see where and when the user navigate from the HTML to the SWF (for example) etc.

If in an HTML environment you would use Cookies, in a Flash environment we advise you to use the SharedObject class

var so:SharedObject = SharedObject.getLocal( "_ga" );
var cid:String;

if( !_so.data.clientid )
{
    cid = generateUUID(); //not found so we generate it
    so.data.clientid = cid;   //then we save it
    so.flush( 1024 );
}
else
{
    cid = so.data.clientid; //found so we reuse it
}

For the Client ID generation, the best method we know of is the implementation you can find here google.analytics.utils.generateUUID().

Why best ?