Skip to content

Commit

Permalink
first version started based on traditional tracking code
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwechner committed Nov 1, 2012
1 parent 1dc5e2d commit 7fbc5f4
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions src/webapp/htdocs/Google-Analytics/GA-asynchronous.xslt
@@ -0,0 +1,160 @@
<?xml version="1.0"?>

<!-- INFO: Asynchronous Tracking Code (https://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&utm_id=ad&answer=1008080) -->

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:yanel-xsl="http://www.wyona.org/yanel/xsl/1.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<xsl:import href="../commons/identity-transformation.xsl"/>

<!-- NOTE: Overwrite the GA-key parameter, for example by including this XSLT within another XSLT -->
<xsl:param name="GA-key" select="'UA-xxxxxx-x'"/>

<xsl:variable name="non-asset-URL-suffix" select="'.html'"/>

<xsl:template match="xhtml:body">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:call-template name="yanel-xsl:insert-GA-code"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>



<!-- INFO: Check if URL corresponds to an asset -->
<xsl:template name="yanel-xsl:is-asset-URL">
<xsl:param name="URL"/>

<!-- INFO: Remove query string from URL -->
<xsl:variable name="url_without_qs"><xsl:choose><xsl:when test="contains($URL, '?')"><xsl:value-of select="substring-before($URL, '?')"/></xsl:when><xsl:otherwise><xsl:value-of select="$URL"/></xsl:otherwise></xsl:choose></xsl:variable>

<xsl:choose>
<!-- INFO: Do not track assets on full-qualified (presumably other than the current/local) domains -->
<!--TODO: Check the realm URL in case someone wants to use the full URL (also locally)? -->
<!-- INFO: Commented, because in many cases it makes sense to track full-qualified domains!
<xsl:when test="starts-with($URL, 'http://') or starts-with($URL, 'https://')">
<xsl:text>no</xsl:text>
</xsl:when>
-->

<!-- INFO: Check if URL ends with forward slash (Equals to expression: ends-with($URL, '/')) -->
<xsl:when test="substring($url_without_qs, string-length($url_without_qs)) = '/'">
<xsl:text>no</xsl:text>
</xsl:when>


<!-- NOTE: Check if an URL does NOT end with .html and hence assume it is an asset. At the moment the suffix is only compared with .html (see $non-asset-URL-suffix) and hence .htm and .php is handled exceptionally -->
<!-- At the moment the following cases are not checked: .htm, foo-bar/, foo-bar -->
<!-- TODO: find a better method to differentiate document assets (e.g. pdf, doc) and regular pages! -->
<!-- NOTE: As a workaround we just hardcode all other suffixes which shall be excluded! -->
<xsl:when test="(substring($url_without_qs, 1 + string-length($url_without_qs) - string-length($non-asset-URL-suffix)) != $non-asset-URL-suffix) and (substring($url_without_qs, 1 + string-length($url_without_qs) - string-length('.htm')) != '.htm') and (substring($url_without_qs, 1 + string-length($url_without_qs) - string-length('.php')) != '.php') and (substring($url_without_qs, 1 + string-length($url_without_qs) - string-length('#')) != '#')">
<!-- Equals to expression: not(ends-with($url_without_qs, $non-asset-URL-suffix)) -->
<xsl:text>yes</xsl:text>
</xsl:when>

<xsl:otherwise>
<xsl:text>no</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>



<!-- INFO: Add onclick if URL corresponds to an asset -->
<xsl:template match="*[local-name()='a']">
<!-- TODO: It seems that all links inserted with TinyMCE are loosing their namespace and hence the below does not match
<xsl:template match="xhtml:a">
-->
<xsl:param name="URL" select="@href"/>

<!-- DEBUG: Output URL suffix
<xsl:attribute name="suffix"><xsl:value-of select="substring($URL, string-length($URL) - string-length($non-asset-URL-suffix) + 1)"/></xsl:attribute>
-->

<xsl:variable name="is-asset">
<xsl:call-template name="yanel-xsl:is-asset-URL">
<xsl:with-param name="URL" select="$URL"/>
</xsl:call-template>
</xsl:variable>

<xsl:choose>
<xsl:when test="$is-asset = 'yes'">
<xsl:call-template name="yanel-xsl:put-GA-asset-onclick-handler">
<xsl:with-param name="URL" select="$URL"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:apply-imports/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="yanel-xsl:insert-GA-code">
<xsl:call-template name="yanel-xsl:insert-new-GA-code"/>
</xsl:template>


<xsl:template name="yanel-xsl:insert-new-GA-code">
<script type="text/javascript"><xsl:text>
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:text></script>


<script type="text/javascript"><xsl:text>
var pageTracker = _gat._getTracker("</xsl:text><xsl:value-of select="$GA-key"/><xsl:text>");
pageTracker._trackPageview();

<!-- INFO: Remove http:// or https:// and hostname from asset URLs -->
<!--TODO?(performance) move that into a separate JS file: -->
function Yanel_requestURIFromFQURL(HTMLAelement) {
var FQURL = HTMLAelement.href<!--alert('FQURL: ' + FQURL);-->;
var i = FQURL.indexOf('://');<!--alert('i: ' + i);-->
i = FQURL.indexOf('/', i + 3);<!--alert('i: ' + i);-->
var filename = FQURL.substring(i);
<!-- DEBUG: In oder to see this alert, make sure to comment/uncomment the code line where this function Yanel_requestURIFromFQURL is used
alert('DEBUG: filename: ' + filename); return false;
-->
return filename;
}
</xsl:text></script>

</xsl:template>


<xsl:template name="yanel-xsl:put-GA-asset-onclick-handler">
<xsl:param name="URL"/>

<xsl:copy>
<xsl:apply-templates select="@*[name() != 'onclick']"/>
<xsl:attribute name="onclick">

<!-- DEBUG: Uncomment the following call and comment the call with the pageTracker in order to debug the function Yanel_requestURIFromFQURL
<xsl:call-template name="yanel-xsl:GA-asset-filename-JSexpr-from-URL">
<xsl:with-param name="URL" select="$URL"/>
</xsl:call-template>
-->
<xsl:text>javascript: pageTracker._trackPageview(</xsl:text>
<xsl:call-template name="yanel-xsl:GA-asset-filename-JSexpr-from-URL">
<xsl:with-param name="URL" select="$URL"/>
</xsl:call-template>
<xsl:text>);</xsl:text>

<xsl:value-of select="@onclick"/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>


<xsl:template name="yanel-xsl:GA-asset-filename-JSexpr-from-URL">
<xsl:param name="URL"/>
<xsl:text>Yanel_requestURIFromFQURL(this<!--, '</xsl:text><xsl:value-of select="$URL"/><xsl:text>'-->)</xsl:text>
</xsl:template>


</xsl:transform>

0 comments on commit 7fbc5f4

Please sign in to comment.