Scalar 2 User's Guide

Adding Google Analytics tracking (Javascript)

The toughest part of Google Analytics is navigating the Google Analytics' Dashboard. The goal is to create a new "property" for the domain of the Scalar book. This will generate an embed snippet that looks like this:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ABCDEFGHIJK"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push( arguments);}
  gtag('js', new Date());
  gtag('config', 'G-ABCDEFGHIJK');
</script>


In a typical website, you would place this at the top of your site's HTML, inside the head tag. For Scalar, the principle is the same, however, you'll place it in the Custom JavaScript box in Scalar's Dashboard. The problem is, however, that the Custom JavaScript bo in Scalar's Dashboard expects pure JavaScript—HTML tags are not allowed—so the embed snippet above needs to be adjusted a little bit.

First, make note of the Google Analytics code that is present in two places in the above snippet. For this example, that code is "G-ABCDEFGHIJK". Take that code and place it into the following, adjusted snippet:

$.getScript("https://www.googletagmanager.com/gtag/js?id=G-ABCDEFGHIJK", function() {
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'G-ABCDEFGHIJK');
});



Now, take this adjusted snippet and cut-and-paste into the Custom JavaScript box in Scalar's Dashboard. That's it! You should start seeing results in Google Analytics after 12-24 hours.

This page has paths: