Integrating Google Analytics

If you want to track your page with Google Analytics, you must first make some settings on your Google account. At the end of the process you will receive from Google a Javascript fragment, which you are to integrate into your website.

The special thing about tracking scripts compared with other Javascript files is that tracking scripts are usually to be integrated last before the closing <body>- tag. All other Javascript files are generally found in the <head> section of the page.

Approach

In the following, we will place the Javascript fragment of Google into its own method. This will then be integrated before the closing <body> tag via a “c.literalCall ()”.

Method »googleAnalytics«

Create a literal method »googleAnalytics« on as general a data type as possible. This has the advantage that the method is generally applicable. Since we do not want to insert any page-specific contents, you can enter the data view »no-data«. The access protection can remain »internal«.

You now copy the Javascript snippet of Google, including the <script> tags, into the method. This then looks roughly as follows:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="/">
    <script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}
    </script>    
  </xsl:template>
</xsl:stylesheet>

In order to avoid page effects in browsers where Javascript is deactivated, one usually inserts HTML comments between the <script> tags and the actual Javascript code. In order to create such an HTML comment in the generated website, the <xsl:comment> element must be used in XSLT. You must ensure that the Javascript fragment respects the shapliness of the Xml document. It is much easier with the indication of a CDATA section. In this way, each script can be inserted without further adjustments.

The method now looks as follows.

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="/">
    <script type="text/javascript">
      <xsl:comment>
//<![CDATA[
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//]]>
      </xsl:comment>
    </script>
    <script type="text/javascript">
      <xsl:comment>
//<![CDATA[
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}
//]]>
      </xsl:comment>
    </script>    
  </xsl:template>
</xsl:stylesheet>

Only in live RenderEngine

If you have configured different RenderEngines for your preview and live page, then you will usually only want to track access to the live page. Anything that happens on the preview is not usually of interest for the evaluations. The script would now be integrated everywhere however.

In order to hide the two <script> tags in the live environment, including their contents, the editing extension can be reverted to. Add the namespace »http://onionworks.net/2006/data/editing« and extend the two opened <script> tags by the attribute

edit:ifEnabled="removeNode"

Integrating into HTML structure

Since everything is now prepared, the script just needs to be integrated into the HTML before the closing <body> tag. For this purpose, insert the following code at exactly this point:

<c.literalCall method="googleAnalytics" />

If you would like to call the method not on each page but on a particluar ID, you must transfer it to the “literalCall” via the attribute »id«.

If you structure the basic structure of your page in only one “default()” method, you must integrate the line only once. Otherwise you should insert the call in each method which contains an HTML structure and is to be analyzed by Google.