-
Notifications
You must be signed in to change notification settings - Fork 5
/
GA.xslt
160 lines (122 loc) · 6.4 KB
/
GA.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?xml version="1.0"?>
<!-- INFO: Traditional Tracking Code (https://support.google.com/analytics/bin/answer.py?hl=en&answer=1012283&topic=1008082&ctx=topic) -->
<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>