Using Google Analytics

Here are just some notes I am making as I try to work out how to use this.

It seems that you sign up to google analytics with your google account.

Then you can add websites to be analysed.

Useful website: https://moz.com/blog/absolute-beginners-guide-to-google-analytics

You add analytics account - you can have more than one. Eg we have New Zealand Digital Library account.

Inside your account, you can have lots of properties, which are websites.

Once you set up a property for a website, you get a snippet of code which goes into the head of all the pages in that website that need tracking. The code is common to all pages apart from the ID, which is unique to a property. In the following example, I have put UA-xxx as a substitute for the tracking ID.

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

  gtag('config', 'UA-xxx');
</script>

If you are writing this into XSLT file, you need to format it like this:

  <xsl:template name="addGoogleTracking">
    <xsl:variable name="trackid">UA-xxx</xsl:variable>
    <!-- Global site tag (gtag.js) - Google Analytics -->
	<script async="async" src="https://www.googletagmanager.com/gtag/js?id={$trackid}"><xsl:text> </xsl:text></script>
	<script>
	  <xsl:text disable-output-escaping="yes">
	  window.dataLayer = window.dataLayer || [];
	  function gtag(){dataLayer.push(arguments);}
	  gtag('js', new Date());
	  
	  gtag('config', '</xsl:text><xsl:value-of select="$trackid"/><xsl:text disable-output-escaping="yes">');</xsl:text>
	</script>
  </xsl:template>

Adding and using new servlet params

To make this work I have modified the java code to store any initParams mentioned in the servlet definition (in servlets.xml) in the config_params HashMap. This gets passed to the transformer when it does the page transformation, so all of these get passed in as parameters. You can access them by declaring <xsl:param name="param-name"/> at the start of web/interfaces/default/transform/gslib.xsl along with the other predefined ones.

  <!-- some global parameters - these are set by whoever is invoking the transformation
  Any servlet init parameters can be accessed by adding a param here-->
  <xsl:param name="interface_name"/>
  <xsl:param name="library_name"/>
  <xsl:param name="site_name"/>
  <xsl:param name="use_client_side_xslt"/>
  <xsl:param name="filepath"/>
  <xsl:param name="google_tracking_id"/>

Now that this has been implemented, instead of hard coding the tracking id in the xslt file, you can specify it as an initParam to a servlet (therefore each servlet can have a different one if need be)

In web/WEB-INF/servlets.xml, add an init-param like the following to the main library servlet (specifying of course, your actual google id):

	<init-param>
	  <param-name>google_tracking_id</param-name>
	  <param-value>UA-xxx-1</param-value>
	</init-param>

You will need to restart the server for this to take effect.