Rocket Loader Webtrends error

rocketloader

#1

We use a company called Webtrends for analytics in which we add two JavaScript files to our sites:

https://www.ao-business.com/scripts/webtrends.min.js
https://s.webtrends.com/js/webtrends.hm.min.js

We get the following error:

rocket-loader-webtrends-failure

Something tells me we’ll have to ignore Webtrends when using Rocket Loader:


#2

Hi @austin.davies,

Leave this with me and we’ll take a look for you.


#3

OK - so this is Rocket Loader exposing a pre-existing issue in your code, the reason is that dcsMultiTrack() is set to be called when the load event fires:

<script type="text/javascript">
        var showLiveChat = true;

        window.addEventListener('load', function() {
            var supportUrl = 'https://support.ao.com/';
            dcsMultiTrack('DCS.dcssip', supportUrl, 'DCS.dcsuri', supportUrl, 'WT.z_livechat', 'NanorepOffered', 'WT.dl', '99');
        });
    </script>

However, dcsMultiTrack() is defined in webtrends.min.js which is dynamically injected as an asynchronous script in an inline script block further down the page:

(function(){
        var s = document.createElement("script"); s.async = true; s.src = "/scripts/webtrends.min.js";
        AddRunatServerAttibute(s);        
        var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2);
    }());

The key problem here is you’re asking the browser to load web trends async with s.async = true - but you’re calling dcsMultiTrack() on load, which means there’s a race condition. Rocket Loader defers the loading of the scripts so the load event will fire much faster. So this issue is technically present without Rocket Loader, but as you have lots of elements delaying the load event, the race condition probably never emerges without Rocket Loader.

What you should probably do here is remove that first script block entirely, and leave the script as asynchronous and trigger your dcsMultiTrack() call based on that script loading:

    (function(){
        var s = document.createElement("script"); s.async = true; s.src = "/scripts/webtrends.min.js";
        AddRunatServerAttibute(s);        
        var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2);

        window.showLiveChat = true;

        s.onload = function() {
            var supportUrl = 'https://support.ao.com/';
            dcsMultiTrack('DCS.dcssip', supportUrl, 'DCS.dcsuri', supportUrl, 'WT.z_livechat', 'NanorepOffered', 'WT.dl', '99');
        };
    }());

That should work without any race conditions and behave well both with & without Rocket Loader.


#4

Thanks for the thorough answer Simon - all makes total sense.


#5

No problem - I should note that Peter (author of https://blog.cloudflare.com/too-old-to-rocket-load-too-young-to-die/ ) helped me a bunch with tracking this one down :+1:


#6

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.