A long-standing issue with Google Analytics is its problems with using cookies for storing the ID of a particular visitor across page views. This is problematic for embedded widgets that often work within an iframe, where e.g. Safari per default disallows cookies so called 3rd party cookies within iframes. A widget maker is only left with the option of injecting GA on the parent page and risk a collission with the site owner’s own GA.

We have come up with a technique for tracking widgetviews, instead of pageviews, of our widget across sites and in most modern browsers.

How we do it
Tracking a visitor is done by generating a unique clientId, i.e. a random and unique ID, that is used as a name for a particular visitor (or browser session, that is). To track views across a site this clientId must be persisted somehow. Per default GA previously stored this in a domain cookie, and there are a multitude of techniques for “passing on” this piece of information across site-borders.

The new Universal Analytics provides the ability for the developer to keep track of this clientId themselves. So, if we just had a JavaScript function that gives us a unique ID that is persisted for a viewer of our widget, across sites — we’re home.

Here it is:

To initialize GA in the widget, you make this create call instead:

In regards to user privacy I’d argue that this is better than many other solutions. We store our cookies on a single domain specific to our widget rather than shadily passing it on from domain to domain.

I’ve simplified our actual implementation of this somewhat in this post, so if you want to know more just tweet me @emilisto.

Isn’t it wrong to track people across sites?
By-passing the cross-domain policy of tracking begs for a few words on privacy. The illicit cross-domain tracking that has been much called out usually takes the form of site-owners tracking users across seemingly unrelated sites to gather and correlate data in ways the user could not foresee. Adding cross-site tracking to an embedded widget is on the other hand different in an important way: if the same widget is embedded on two different site the visitor can actually see that this is a shared piece of content and if he or she accepts being tracked on one site, tracking a single widget use across sites is analogue to tracking a user across a single site.