Search code examples
javascriptgoogle-analyticstrackingmatomo

is it good to delay run of tracking code


I know tracking codes for piwik, google analytic and other similar stuff, placed at the end of page to not affect page rendering. But there is a minor problem that the page still not complete. The progress indicator in user browser still cycling! If you test your page (which contain tracking code) with tools like WebPageTest you see the overall time is count the tracking code requests (at least 2 request one for JS and another for tracking).

I make a simple delay by using window.setTimeout(trackingfunction, 3000). The tracking code will fired three seconds after the line executed! Guessing the page is completely done at that time. It lead to indicator stop just after page load and don't cycle again when calling tracking code. In the tools also the tracking code not shown and the reported time is page real time.

Does it correct tracking? Does it track javascript-enabled bots?

PS:
I read this article, he also do somthing to defer the loading of thirdparty javascripts. but i cant decide does his work is correct or not?


Solution

  • There are pros and cons to deferring the loading of the Google Analytics JS file until after the load event fires. Ultimately, what you decide to do is based on the needs of your individual site. There's not a one-size-fits-all answer here.

    Pros

    • The load event for your page will happen quicker. This will (as you mentioned) mean the spinner is not spinning for as long, and performance analysis tools will report your page as loading faster.
    • Google also (supposedly) favors pages that load faster, and this could help with SEO (again, possibly).

    Cons

    • If a user leaves before the page finishing loading, Google Analytics will not know about them. If you have a lot of users who get to your site and then leave almost immediately, there could be a reason they're doing that, and if you don't know it's happening, you can't try to fix the problem.
    • Similar to the above point, if bouncing users are not being counted, your bounce rate may artificially low, and any business decisions you make based on your bounce rate may be affected.
    • If you delay loading and you have any references to the ga() function, you run the risk of those calls generating errors. If you're going to delay the downloading of the analytics.js script, at minimum, you should run the ga() initialization code in the <head> since it's very small and its performance affect will be negligible.
    • If there are any JavaScript errors on the page, they may prevent Google Analytics from loading at all (depending on how you organize/structure you JS). Part of the reason Google Analytics suggests putting their snippet in the <head> is to make sure it's loaded as quickly as possible and avoid the problem of errors preventing it from being loaded.

    Bottom line: Yes, it's OK, but only do it if you know what you're doing, you understand the implications, and you're aware of the downsides and don't care about them.

    Update

    Ilya Grigorik wrote a good article about how <script async> address some of your concerns about this. You should also note that Google Analytics has a recommended snippet that uses <script async> as well.