
Show HN: Minimal Google Analytics Snippet - davidkuennen
https://minimalanalytics.com
======
davidkuennen
Created this, cause I hated having to slow down my websites by adding the
gigantic Google Tag Manager and Analytics libraries, combined with their
randomly slow CDN, just to track some basic page views.

~~~
jopsen
I'm not an expert, just curious... But aren't these files cached on most users
computers?

~~~
jasonkester
Nothing is ever really cached anymore, meaningfully at least.

We're in an age where websites happily serve up 8mb JavaScript bundles to
display their static homepage to a mobile browser that has maybe a couple
hundred MB allocated to caching things. Sure, it may be there if you
immediately load another page on that site (and the devs aren't using a
bundler that serves a _different_ 8mb next time).

But if they leave and come back, they'll get the same full download
experience.

~~~
jefftk
For what it's worth, I run a/b tests in my work where I consistently see large
caching effects. For example, when we compare an experimental version of
something to the currently deployed version it's critical that we cache-bust
the control group. And changes that improve caching generally make things
significantly better.

Caching effects are much smaller on Safari with its per-domain cache, but even
there if something broke caching for us it would be a high priority to fix.

(I work at Google on ad tags)

------
denormalfloat
Small piece of history: I used to work on Analytics and they would give out
swag. One piece was a T-shirt with the entire tracking snippet on it. It was a
great conversation starter.

Sad to see that Tag Manager and the currently tracking JS are big enough to
force website owners to compromise.

~~~
louismerlin
They are also probably too big to put on a t-shirt !

~~~
knewter
I doubt it:
[https://www.litographs.com/collections/t-shirts/genre_shakes...](https://www.litographs.com/collections/t-shirts/genre_shakespeare)?

------
gobengo
It's cool that in also works with SPA frameworks. Thanks for sharing.

In other surveilance news, I've been adding Matomo[1] for web analytics to my
site for the first time. Used to be called Piwik? It's free/selfhostable web
analytics, which is more than enough for small projects and landing pages.

I've been pleasantly surprised with how it works so far, but I had to write
some code to trigger it in the right part of my react-router initialization.

[1] [https://permanent.cloud/apps/matomo](https://permanent.cloud/apps/matomo)

(and its cool I'm not helping Google spy on people who visit and help me out)

~~~
siddhant
For the surveillance conscious I can also recommend using Fathom
([https://usefathom.com](https://usefathom.com)). I’ve been using it for a few
weeks now and have only good things to say.

~~~
Cenk
Have you tried Mamoto / Piwik too? I’m thinking of switching to Fathom and I’m
not sure if it’s worth the effort.

~~~
siddhant
I haven't tried Mamoto/Piwik, but from the looks of it they look like they
have a pretty heavy feature set. If it works for you, I don't see any reason
to switch. But if you prefer something that's intentionally low on features
and "minimal", Fathom is pretty cool.

------
RodgerTheGreat
While you're at it, why not be less invasive and neuter some of the data you
send across? Does Google need to know about screen colordepth, language
settings and text encodings, or fine-grained viewport dimensions, or is that
stuff just bits of entropy to fingerprint users? Hardcode it!

~~~
davidkuennen
Added options for the information you want to track.

------
nwellnhof
The snippet seems to circumvent the Google Analytics Opt-out Browser Add-on
which many sites mention as a way to disable GA tracking in their privacy
policy. So if you use the snippet, don't tell your users that they can opt out
with the add-on.

~~~
davidkuennen
Are you sure it circumvents it? My uBlock Origin is still properly blocking
the API call to google analytics. But since the Add-on is from Google itself
it could work in different ways indeed.

~~~
nwellnhof
Yes, the add-on seems to work differently. I suspect that analytics.js somehow
checks for its presence and stops collecting data if it was found.

~~~
davidkuennen
If someone finds out whatever global variable that add on sets I could add it.

~~~
gorhill
It creates a small inline script with this code:

    
    
        window["_gaUserPrefs"] = { ioo : function() { return true; } }
    

I used CRX Viewer extension by Rob Wu[1] to just look up the extension code in
Chrome store.

[1] [https://github.com/Rob--W/crxviewer#chrome-extension-
source-...](https://github.com/Rob--W/crxviewer#chrome-extension-source-
viewer-crx-viewer)

------
latchkey
Could you make it a tiny bit smaller by aliasing `window` and `document` and
`localStorage` to remove the duplicated calls? `var w=window;var
d=document;var l=localStorage;`

Interstingly, copying/pasting the code into
[https://www.minifier.org/](https://www.minifier.org/) gains 69b of savings,
but [https://jscompress.com/](https://jscompress.com/) shows an error:
`Invalid regular expression: /+/: Nothing to repeat (line: 3, col: 48)`

~~~
s_y_n_t_a_x
[https://closure-compiler.appspot.com/](https://closure-compiler.appspot.com/)
shaves off 9% w/ no errors

------
FoxInBoxers
Very cool, thank you!

Would you expect Google's API to change often? My biggest concern with
something like this is their /collect endpoint changing and analytics data
stops being tracked.

~~~
davidkuennen
It's officially documented here:
[https://developers.google.com/analytics/devguides/collection...](https://developers.google.com/analytics/devguides/collection/protocol/v1/reference)

So I think they won't change it or just add a different version with the old
version still working.

------
largehotcoffee
Can you include this?

ga('set', 'anonymizeIp', true);

~~~
davidkuennen
Will add an option for that as soon as I'm home. Also got many feature
requests for exposing an event function. Both of these things shouldn't add
any significant size to the snippet.

------
franciscop
I get a SyntaxError when embedding it:

> SyntaxError: nothing to repeat

The line seems to be this:

> return l?l[2]?decodeURIComponent(l[2].replace(/+/g,' ')):void 0:void 0},

Might it be related to AdBlock? I'm using Firefox with AdBlockPlus, Cookiebro
and the default Privacy settings turned up in Firefox.

Edit: oops, it's clearly coming from the regex: /+/g → /\\+/g

[https://stackoverflow.com/a/6288596](https://stackoverflow.com/a/6288596)

~~~
davidkuennen
Thanks for sharing! Will look into it.

~~~
franciscop
See the edit, I found the bug and the solution :)

~~~
davidkuennen
Great! Thanks.

------
microcolonel
You can also pretty reasonably remove "colorDepth:true", I can not possibly
imagine you have many indexed or RGB545 displays going to your site these
days.

------
jrockway
Isn't the tag manager and analytics library likely to be cached, thus using
zero bytes on average versus the non-zero number of bytes for this library?

~~~
rovr138
A quick check, GTM is only cached for 15mins.

If users spend a bit of time navigating your site, you might get better
caching by adding this to your JS file and caching for longer.

~~~
seanwilson
That only means it needs to check if there's a new version every 15 minutes
though (e.g. by file hash or modification date), not that it has to download
it again even if there's no changes every 15 minutes. I haven't looked into
the cache settings though.

------
LolNoGenerics
Now I am intrigued to make a minimal snippet that creates a histogram of
loading times.

------
davidkuennen
Thank you all for your great feedback. I made some improvements based on that.

Now using navigator.sendBeacon if available (not even GA does that by default
yet), ability to track events and ability to track exceptions.

All of that by keeping it tiny.

------
reaperducer
When I see the hoops people have to jump through to get basic privacy from
Google, it makes me happy that my company's legal department prohibits us from
using Google products for HIPAA reasons.

------
tootie
Slight tangent: What's folks' feeling about GTM vs Tealium or Segment?

------
ieq8
could you create github repo?

~~~
davidkuennen
Have now added the original Snippet Code as GitHub gist to the website.

------
pspeter3
Is there a reason you are not using the beacon API for this work?

~~~
davidkuennen
Didn't know about it until now. Will look into it, thanks!

~~~
lmkg
Note that Google Analytics has an option to use the Beacon API, somewhere in
it. Re-using that might save you some work.

------
revskill
Any chance to put this in a web worker ?

------
zyx321
First impulse: "That sounds like a great way to get your GA account banned for
using their API in undocumented ways."

~~~
davidkuennen
They have it documented here:
[https://developers.google.com/analytics/devguides/collection...](https://developers.google.com/analytics/devguides/collection/protocol/v1/reference)

So I think it's a totally legit way to use GA, since you don't always have the
ability to add libraries. For example if you want to track GA server side.

------
ApolloRising
FYI: On your site you use snipped, you may want to consider using the word
snippet instead.

[https://en.wikipedia.org/wiki/Snippet_(programming)](https://en.wikipedia.org/wiki/Snippet_\(programming\))

~~~
davidkuennen
Fixed it. Thank you.

