最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do websites like Hotjar and Google Analytics use complex tracking code instead of just a <script>

programmeradmin3浏览0评论

Website that use JS tracking usually use this kind of code :

<script>
    (function(h,o,t,j,a,r){
        h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
        h._hjSettings={hjid:9999,hjsv:5};
        a=o.getElementsByTagName('head')[0];
        r=o.createElement('script');r.async=1;
        r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
        a.appendChild(r);
    })(window,document,'//static.hotjar/c/hotjar-','.js?sv=');
</script>

Website that use JS tracking usually use this kind of code :

<script>
    (function(h,o,t,j,a,r){
        h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
        h._hjSettings={hjid:9999,hjsv:5};
        a=o.getElementsByTagName('head')[0];
        r=o.createElement('script');r.async=1;
        r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
        a.appendChild(r);
    })(window,document,'//static.hotjar./c/hotjar-','.js?sv=');
</script>

In the end, those scripts just add a <script> tag to the <head> of the page, so surely there must be a reason why they're doing it this way.

Is it for ad-blocking bypass reasons ? Wouldn't the generated request be the same as if it was hardcoded in the <head> ?

Share Improve this question asked Aug 24, 2017 at 12:17 Olivier DelaunayOlivier Delaunay 511 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

I'm the chief architect at Hotjar so I'll explain the reasons why we did it in this particular way.

  1. We need to do things before the main script is loaded.

    h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};

That particular line allows us to store actions to execute once the main script is loaded. It allows for things like hj('trackVirtualPageView', '/url') to be called before our script is loaded.

  1. We can store things like settings as part of the snippet.

    h._hjSettings={hjid:9999,hjsv:5};

That could absolutely be added as part of the query string when loading the script. The downside of using that approach is that we would get less optimal caching since it would be impossible for a browser to know that script.js?hjid=1 and script.js?hjid=2 actually loads the same JS file.

  1. What we're doing in the last part is actually just creating a <script async=1> tag and adding it to the <head> which works really well. The reason we're doing it through JS is that we like to make it as easy as possible for our users by only asking them to put code in one place.

There might be an even better to do what we're doing which I'm blissfully unaware of, and in case there is, please reach out and tell me about it! :)

At least part of the answer is that vendors want to load their libraries in a way that does not block page rendering.

If the browser hits a script element it tries to get the script source, and might prevent the page from rendering until the plete script is downloaded. In the bad old days it used to happen that website would show up blank, because the (then synchronous) Google Analytics script could not be downloaded in a timely fashion and stopped the page from rendering. Script injection became an accepted method to make scripts non-blocking.

There are other ways (defer, asynch, etc - for historical interest here is a link to an 2009 article that discusses the issue, because the problem is that old), but script injection is a convenient way to set up a few variables along the way (plus if Google does it it must be the best way, or so seems to be the though process with some panies).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论