Dynamic Tracking Code Loading in HTTPS or HTTPThe following is a guest blog from Gustavo Cornejo of Multiplica, one of the members of our Consultant Network (YWACN). They are located in Barcelona and specialize in web measurement, testing, and intelligent analysis. You can find the original post in Spanish here. Monitoring websites with secure pages requires the use of two different sets of Yahoo Web Analytics tracking code, one for secure pages and the other for non-secure pages. The drawback to this is that you are left with inconsistent tracking code across the site and care must be exercised to ensure that the correct tracking code is being used in the right place. An alternative to this would be to use the secure tracking code on all pages but this would lead to unnecessary load time on the pages that do not need it. Another solution is to integrate a discriminator according to the protocol used on the page to load only the code necessary for that page. Additionally, we must know when the JavaScript is loaded and execute the tracking code without causing execution errors. The solution is taken in 4 steps:
TrackPageCode:
function TrackPage(docName, docGroup){
var _yat;
if(typeof(docName)=="undefined") docName="";
if(typeof(docGroup)=="undefined") docGroup="";
try{
_yat = YWA.getTracker("1000621731362");
_yat.setDocumentName(docName);
_yat.setDocumentGroup(docGroup);
//_yat.setDomains("*.metriplica.com");
//_yat.setCookieDomain("www.metriplica.com");
//_yat.setMemberId("");
_yat.submit();
}catch(err){
if(typeof(console)=="undefined")
console.log("Error tracking Yahoo: %o", err);
};
};
Explanation: Dynamic loading of JavaScriptCode:
(function(d, t){
var s, h;
s = d.createElement(t);
s.type = 'text/javascript'; s.async = 1;
s.src = ('https:' == d.location.protocol ? 'https://s' : 'http://d') + '.yimg.com/mi/ywa.js';
h = d.getElementsByTagName(t)[0];
h.parentNode.insertBefore(s, h);
})(document, 'script');
Explanation: And for the non-secure case: Execute TrackPageCode:
s.onreadystatechange = function () {
if (js.readyState == 'complete') {
TrackPage();
}
}
s.onload = function () {
TrackPage();
};
Explanation: NoScriptCode: <noscript> <div><img src="https://a.analytics.yahoo.com/p.pl?a=10001502141662&js=no" width="1" height="1" alt="" /></div> </noscript> Explanation: The complete code
<script type="text/javascript">
function TrackPage(docName, docGroup){
var _yat;
if(typeof(docName)=="undefined") docName="";
if(typeof(docGroup)=="undefined") docGroup="";
try{
_yat = YWA.getTracker("1000621731362");
_yat.setDocumentName(docName);
_yat.setDocumentGroup(docGroup);
//_yat.setDomains("*.metriplica.com");
//_yat.setCookieDomain("www.metriplica.com");
//_yat.setMemberId("");
_yat.submit();
}catch(err){
if(typeof(console)!="undefined")
console.log("Error tracking Yahoo: %o", err);
};
};
(function(d, t){
var s, h;
s = d.createElement(t);
s.type = 'text/javascript'; s.async = 1;
s.src = ('https:' == d.location.protocol ? 'https://s' : 'http://d') + '.yimg.com/mi/ywa.js';
s.onreadystatechange = function () {
if (js.readyState == 'complete') {
TrackPage();
}
}
s.onload = function () {
TrackPage();
};
h = d.getElementsByTagName(t)[0];
h.parentNode.insertBefore(s, h);
})(document, 'script');
</script>
<noscript>
<div><img src="https://a.analytics.yahoo.com/p.pl?a=10001502141662&js=no" width="1" height="1" alt="" /></div>
</noscript>
Using the above code allows us to use the same code for all pages, and also provide a generic function to keep track of additional actions within the page. |
|