Minimal Google Analytics Snippet

A simple snippet for tracking page views on your website.

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.

Reviews
Discussion
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 google-analytics.com domain, is that true?
@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 (https://news.ycombinator.com/ite...) I will also add an option to anonymize the IP address.
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.
@panphora Thanks for your feedback 😺 Added options now. Will also open source the snippet on GitHub.
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! 😻
Thanks for all your upvotes! Already got many feature requests for adding an event function.
This is nice! Might add it as an option to www.getsjabloon.com 👌
Very interesting, thanks David.