Enable User Tracking in Google Analytics without User Accounts

Joined
Apr 5, 2017
Messages
133
Likes
90
Degree
0
I'm using @Nat's super simple, affiliate masking code- which was so clean I was able to do extend functions well beyond Amazon. Consider this payment in kind.

Here's how you can enable user-id tracking without having to setup user accounts on your CMS, in just one (or two) lines.

Code:
$.getJSON('https://freegeoip.net/json/', function(data) {
        ga('set', 'userId', data.ip);
}

I already use freegeoip for other things, but you can use other ip services like ipinfo.io or whatever to work too.
 
This is a decent way to track users via IP.
If you are not already using freegeoip or a similar service, you can save an http request by getting the ip from your server side language, eg in php:

Code:
var userIp = '<?=$_SERVER['REMOTE_ADDR']?>';
ga('set', 'userId', userIp);

Now im not posative on this point, but it seems likely that this is going to overwrite the default custId that analytics set via cookies, so in cases where you have multiple users using the same ip (eg college campuses), you could be polluting your analytics data.

With that in mind, its probably better to use a custom tracking field rather than userid:

https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets

You can also use browser fingerprinting to identify individuals:

https://github.com/Valve/fingerprintjs2

Code:
new Fingerprint2().get(function(result, components){
    var userFingerprint = result;
    ga('set', 'userId', userFingerprint);
});

Now of course as mentioned earlier, analytics uses cookies to do this by default, so this only really catches those that clear cookies, use incogito/private browsing or have other non tracking plugins attached.

As a side note, this type of fingerprinting can be used for lots of things, such as reducing the ability of cheapskates abusing free trial accounts
 
Last edited:
Now of course as mentioned earlier, analytics uses cookies to do this by default, so this only really catches those that clear cookies, use incogito/private browsing or have other non tracking plugins attached.

Unless of course that user is blocking GA with their ad blocker, since those visitors don't show up at all unless you look at your server logs. Some ad blockers do this by default, such as uBlock Origin while others have to be enabled.
 
@yuckystuff Unless of course you mask the link to the analytics.js file, or host it locally.

@Steve I'm not certain if I want to expect to have micro concise data coming from such a quick hack (for specific user targeting). I'm not in the technology buying niche where the visitors could be a CTO in shopping mode.

But from a macro perspective- being able to get a general collective view, naturally grouped by ip is good enough for me, for now. And if that means a collective view of what a college campus surfs, even better.

+--+ I'm a few. good drinks in. Ignore the above. I understood what you said about muddling the data and did a quick read on the official docs and while they were not specific, it reads pretty conclusively but im happy to be currected

I also looked at fingerprintjs and i m not sure about the finger performace on it's own. Maybe combining the result with the ip address over an encryption would create a more accurate result, more precise than the current implementation but worsre than using user acccounts.

Code:
new Fingerprint2().get(function(result, components){
    var userFingerprint = result;
    got2weeks = window.btoa(result+ip);

    ga('set', 'userId', got2weeks);
});

This doesn't track users across devices still, That can only be done with real user accounts. What say you?
 
Back