User Tools

Site Tools


en:user_advanced:google_analytics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:user_advanced:google_analytics [2018/08/06 21:21] kjdonen:user_advanced:google_analytics [2023/03/13 01:46] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +
 +
 +
 ====== Using Google Analytics ====== ====== Using Google Analytics ======
  
Line 12: Line 15:
  
 You add analytics account - you can have more than one. Eg we have New Zealand Digital Library account. 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.
 +
 +<code>
 +<!-- 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>
 +</code>
 +
 +If you are writing this into XSLT file, you need to format it like this:
 +
 +<code>
 +  <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>
 +
 +</code>
 +
 +===== 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.
 +
 +<code>
 +  <!-- 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"/>
 +
 +</code>
 +
 +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):
 +<code>
 + <init-param>
 +   <param-name>google_tracking_id</param-name>
 +   <param-value>UA-xxx-1</param-value>
 + </init-param>
 +
 +</code>
 +
 +You will need to restart the server for this to take effect.
en/user_advanced/google_analytics.1533590485.txt.gz · Last modified: 2018/08/06 21:21 by kjdon