Async Multiple Piwik Trackers


(Andreas Schnederle-Wagner) #1

Hey there,
I just noticed we still use the old sync Multi Tracker implementation for Piwik.

Sync Code:


<!-- Piwik Statistics -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://*statssserver*/" : "http://*statssserver*/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
	var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
	piwikTracker.setDocumentTitle(document.domain + "/" + document.title);
	piwikTracker.trackPageView();
	piwikTracker.enableLinkTracking();

	var piwikTracker1 = Piwik.getTracker(pkBaseURL + "piwik.php", 11);
	piwikTracker1.setDocumentTitle(document.domain + "/" + document.title);
	piwikTracker1.trackPageView();
	piwikTracker1.enableLinkTracking();
} catch( err ) {}
</script>
<noscript>
	<p><img src="https://*statssserver*/piwik.php?idsite=1" style="border:0" alt="Piwik" /></p>
	<p><img src="https://*statssserver*/piwik.php?idsite=11" style="border:0" alt="Piwik" /></p>
 </noscript>
<!-- End Piwik Statistics Code -->

Now I tried to change it to the async approach for better Pageloading Time as mentioned here http://developer.piwik.org/guides/tracking-javascript-guide#multiple-piwik-trackers - but it simply won’t fire the Tracking Request.

Async Code:


<!-- Piwik Statistics -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://*statssserver*/" : "http://*statssserver*/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
window.piwikAsyncInit = function () {
	try {
		var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
		piwikTracker.setDocumentTitle(document.domain + "/" + document.title);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();

		var piwikTracker1 = Piwik.getTracker(pkBaseURL + "piwik.php", 11);
		piwikTracker1.setDocumentTitle(document.domain + "/" + document.title);
		piwikTracker1.trackPageView();
		piwikTracker1.enableLinkTracking();
	} catch( err ) {}
};
</script>
<noscript>
	<p><img src="https://*statssserver*/piwik.php?idsite=1" style="border:0" alt="Piwik" /></p>
	<p><img src="https://*statssserver*/piwik.php?idsite=11" style="border:0" alt="Piwik" /></p>
</noscript>
<!-- End Piwik Statistics Code -->

Everything within window.piwikAsyncInit = function () {}; just never get’s executed?!
We are using latest Piwik 2.13.1.

Hope someone can help me on this?

Thank you, bye from Austria
Andreas Schnederle-Wagner


(Andreas Schnederle-Wagner) #2

alright - after reding through the piwik.js Source I noticed that I need to load the piwik.js file AFTER defining the window.piwikAsyncInit = function (){ }; … makes sense … :wink:

Maybe you should update http://developer.piwik.org/guides/tracking-javascript-guide#multiple-piwik-trackers Doc and mention this … guess I’m not the only one overlooking this when changing the Code … :slight_smile:

Andreas


(Andreas Schnederle-Wagner) #3

Working Async Multiple Piwik Trackers:


<!-- Piwik Statistics -->
<script type="text/javascript">
window.piwikAsyncInit = function () {
	try {
		var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
		piwikTracker.setDocumentTitle(document.domain + "/" + document.title);
		piwikTracker.trackPageView();
		piwikTracker.enableLinkTracking();

		var piwikTracker1 = Piwik.getTracker(pkBaseURL + "piwik.php", 11);
		piwikTracker1.setDocumentTitle(document.domain + "/" + document.title);
		piwikTracker1.trackPageView();
		piwikTracker1.enableLinkTracking();
	} catch( err ) {}
};
</script>
<noscript>
	<p><img src="https://*statssserver*/piwik.php?idsite=1" style="border:0" alt="Piwik" /></p>
	<p><img src="https://*statssserver*/piwik.php?idsite=11" style="border:0" alt="Piwik" /></p>
</noscript>
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://*statssserver*/" : "http://*statssserver*/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<!-- End Piwik Statistics Code -->