Minimal Google Analytics Snippet

A simple snippet for tracking page views on your website.

get it

A simple snippet for tracking page views on your website without having to add external libraries. Also works for single page applications made with the likes of react and vue.js.




You need to become a Contributor to join the discussion - Find out how.
Adriaan@harianus · Privacy minded maker
This is cool, so you have still the power of Google but more control of what they receive from your users? I think this will also disable tracking because it's not on the domain, is that true?
David Künnen
David KünnenMaker@kunnendavid · Creator
@harianus Thanks for the feedback! I think it does disable tracking, since the response header doesn't seem to have any cookies attached. As suggested in this HN thread ( I will also add an option to anonymize the IP address.
David Miranda
David Miranda@panphora · Founder and Developer, Artisfy
This is such a cool idea and very well implemented! Well done!! 😻 A couple of suggestions: • Annotated Source Code! 📝 Some JS libraries, like underscore.js, have a version of their source code that's un-minified and annotated with explanations for every line. Could you do that too? I'd love to see what each piece does. It would help bring me piece of mind and allow me to modify it in the future. • More options! 🔘 You're using the official Analytics API to implement this, but it's not obvious how I could work with this API to add functionality on top of your snippet. Perhaps you could add some checkboxes that add functionality to the code — or some snippets after the code as suggestions. • Performance! 🚀 Would you recommend loading this script asynchronously? Why or why not? A notice about this would help me figure out the best practices and whether or not I should be adding this to me bundle or including it on the page as is.
David Künnen
David KünnenMaker@kunnendavid · Creator
@panphora Thanks for your feedback 😺 Added options now. Will also open source the snippet on GitHub.
David Künnen
David KünnenMaker@kunnendavid · Creator
Hey Hunters! 👋 I created this yesterday afternoon, since I got some time. 🤔 Problem The problem is, that I have to add the Google Tag Manager and Analytics libraries to my websites, even if I just want to track basic page views. The GTM and Analytics libraries are not only huge (72kB together), but also add two additional requests to my websites. Sometimes it would even slow my website down by over 1 second (mainly caused by GTM). 🛠 Solution As a solution I created this simple snippet I can just add to the HTML of my websites and it's working instantly. It even works for single page applications that have no real page reload, for example applications made with React or Vue.js. It sends page views directly to the Google Analytics API, without the need to add all the extra clutter. Maybe someone else has the same problem. Please let me know what you think! 😻
David Künnen
David KünnenMaker@kunnendavid · Creator
Thanks for all your upvotes! Already got many feature requests for adding an event function.
eelco@eelcojellema · building
This is nice! Might add it as an option to 👌
Adam Faryna
Adam Faryna@adamfaryna
Very interesting, thanks David.