6 ms·
Show HN: Minimal Google Analytics Snippet
- davidkuennen 8y agoCreated 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.
- C1sc0cat 8y agoCompared to all the other stuff that gets puts on websites these days most sites wont notice.
- spiderfarmer 8y agoYou're getting downvoted, probably because your comment is whataboutery at best. You're only relevant when you asume someone with a 6Mb homepage is starting with replacing his Analytics code. A usecase that will probably never occur.
- C1sc0cat 8y agoAh you mean 99.99% of the real websites I consult on then - and those are all major brands.
- jopsen 8y agoI'm not an expert, just curious... But aren't these files cached on most users computers?
- niel 8y agoanalytics.js as served by Google is only cached for two hours.
- yayana 8y agoSo a browser checks its hash up to 12 times a day for the surfer who never sleeps, no matter how many sites they visit.. If you are that concerned then your page views should be sent from the server side.
- jasonkester 8y agoNothing 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 8y agoFor 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)
- reaperducer 8y ago8mb JavaScript bundles to display their static homepage to a mobile browser that has maybe a couple hundred MB allocated to caching things Thanks for that. It explains why even on very high-end new phones, switching between complicated web sites in different tabs causes a reload.
- bzbarsky 8y agoIt depends. Browsers that care about user privacy and tracking are moving to first-party-isolated caching. So GA will be cached for a user on your site only if the user has visited _your_ site before.
- onlyrealcuzzo 8y agoAFAIK, GTM is specifically designed that it can only be cached on your website and it expires every 15 minutes. This is because your UAID is part of the URL to download the GTM snippit. The old-school analytics.js file can be cached, though. Someone correct me if this isn't true.
- onion2k 8y agoBoth of Google's recommended methods of adding Analytics to a website load Google's JS library asynchronously[1]. Including it has no meaningful impact a site's time to first paint or interactivity. There's a good argument to say loading 70Kb of JS you're not using is a bad idea, and if that's the case then your smaller script is an improvement, but there's also a good argument to say Google Analytics is powerful and useful and maybe you should be using the features that those 70Kb give you in order to improve you site in ways the user would notice. I guess it's down to your specific use case. [1] https://developers.google.com/analytics/devguides/collection/analyticsjs/ https://developers.google.com/analytics/devguides/collection...
- awirth 8y ago<script async> does still block the document onload event from firing until it's been executed.
- scratchnsniff 8y agoWhat about defer?
- dwild 8y agoWouldn't the 70kb would be most likely already cached too? Which make it even more lightweight than this minimal script.
- dorgo 8y agoMy firefox never cache anything. Going forth and back in history always reloads the page. That's the reason I have many tabs open - to prevent reloading and waiting. about.cache says: Number of entries: 418 Maximum storage size: 30720 KiB Storage in use: 12698 KiB Storage disk location: none, only stored in memory
- tuananh 8y agono because expiration of google analytics is really low (30m or 2h i think)
- ApolloRising 8y agoFYI: 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 8y agoFixed it. Thank you.
- LolNoGenerics 8y agoNow I am intrigued to make a minimal snippet that creates a histogram of loading times.
- FoxInBoxers 8y agoVery 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 8y agoIt's officially documented here: https://developers.google.com/analytics/devguides/collection/protocol/v1/reference https://developers.google.com/analytics/devguides/collection... So I think they won't change it or just add a different version with the old version still working.
- gobengo 8y agoIt'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 8y agoFor 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 8y agoHave you tried Mamoto / Piwik too? I’m thinking of switching to Fathom and I’m not sure if it’s worth the effort.
- siddhant 8y agoI 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.
- masa331 8y agoI can recommend https://github.com/ankane/ahoy https://github.com/ankane/ahoy for Rails
- napsterbr 8y agoPiwik (matomo) is amazing!
- denormalfloat 8y agoSmall 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 8y agoThey are also probably too big to put on a t-shirt !
- knewter 8y agoI doubt it: https://www.litographs.com/collections/t-shirts/genre_shakespeare https://www.litographs.com/collections/t-shirts/genre_shakes...?
- zyx321 8y agoFirst impulse: "That sounds like a great way to get your GA account banned for using their API in undocumented ways."
- davidkuennen 8y agoThey have it documented here: https://developers.google.com/analytics/devguides/collection/protocol/v1/reference https://developers.google.com/analytics/devguides/collection... 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.
- sudhirj 8y agoGA has had an open tracking API for a while, it’s what powers tracking inside mobile apps, custom events, server side tracking etc.
- jrockway 8y agoIsn'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 8y agoA 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.
- yayana 8y agoGTM used to load async, but they wanted to support A/B testing.. If you aren't using A/B testing then you would get perfectly good performance by using the older GTM snippet, it will just continue to use bandwidth at these 15 minute intervals. Even then it probably will only test the hash to see if the container changed.. This seems to me like one real problem that is pretty trivial to fix leading to a lot of over tuning for some kind of ideal.. and then being stuck if you need to support anything else.
- seanwilson 8y agoThat 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.
- latchkey 8y agoCould 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)`
- moviuro 8y agoWouldn't gzip encoding (apache/nginx/whatever is used today) compress that anyway?
- s_y_n_t_a_x 8y agohttps://closure-compiler.appspot.com/ https://closure-compiler.appspot.com/ shaves off 9% w/ no errors
- largehotcoffee 8y agoCan you include this? ga('set', 'anonymizeIp', true);
- davidkuennen 8y agoWill 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.
- nwellnhof 8y agoShould be as simple as adding `aip:1` to the parameter list (the `var j={...}` part).
- davidkuennen 8y agoYou can now give the option to anonymize ips. It's also default true.
- OnlyRepliesToBS 8y ago'the smallest ever!' 'can you make it bigger' '...'
- deleted 8y ago[deleted]
- ieq8 8y agocould you create github repo?
- davidkuennen 8y agoSure! Will do so as soon as I'm home.
- davidkuennen 8y agoHave now added the original Snippet Code as GitHub gist to the website.
- franciscop 8y agoI 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 8y agoThanks for sharing! Will look into it.
- franciscop 8y agoSee the edit, I found the bug and the solution :)
- davidkuennen 8y agoGreat! Thanks.
- davidkuennen 8y agoIs now fixed.
- nwellnhof 8y agoThe 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 8y agoAre 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 8y agoYes, 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 8y agoIf someone finds out whatever global variable that add on sets I could add it.
- gorhill 8y agoIt 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-viewer-crx-viewer https://github.com/Rob--W/crxviewer#chrome-extension-source-...
- RodgerTheGreat 8y agoWhile 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 8y agoAdded options for the information you want to track.
- pspeter3 8y agoIs there a reason you are not using the beacon API for this work?
- davidkuennen 8y agoDidn't know about it until now. Will look into it, thanks!
- lmkg 8y agoNote that Google Analytics has an option to use the Beacon API, somewhere in it. Re-using that might save you some work.
- reaperducer 8y agoWhen 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.
- microcolonel 8y agoYou 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.
- revskill 8y agoAny chance to put this in a web worker ?
- tootie 8y agoSlight tangent: What's folks' feeling about GTM vs Tealium or Segment?
- davidkuennen 8y agoThank 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.