javascripteventsanalyticsweb-analytics

What does heap analytics' script code do?


So Heap Analytics tells me to paste this code in order to use their product -

<script type="text/javascript">
  window.heap=window.heap||[];heap.load=function(a){window._heapid=a;var b=document.createElement("script");b.type="text/javascript",b.async=!0,b.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.heapanalytics.com/js/heap.js";var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c);var d=function(a){return function(){heap.push([a].concat(Array.prototype.slice.call(arguments,0)))}},e=["identify","track"];for(var f=0;f<e.length;f++)heap[e[f]]=d(e[f])};
  heap.load("YOUR_APP_ID");
</script>

What does this code do? (barring all the app id stuff).

I found something similar in the open source analytics.js

<script type="text/javascript">
window.analytics||(window.analytics=[]),window.analytics.methods=["identify","track","trackLink","trackForm","trackClick","trackSubmit","page","pageview","ab","alias","ready","group","on","once","off"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var method=window.analytics.methods[i];window.analytics[method]=window.analytics.factory(method)}window.analytics.load=function(t){var a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)},window.analytics.SNIPPET_VERSION="2.0.8",
window.analytics.load("YOUR_WRITE_KEY");
window.analytics.page();
</script>

Is that doing something similar? (at a glance it looks like it)


Solution

  • Here's the snippet as of May 2021:

    window.heap = window.heap || [], heap.load = function (e, t) {
      window.heap.appid = e, window.heap.config = t = t || {};
      var r = document.createElement("script");
      r.type = "text/javascript", r.async = true, r.src = "https://cdn.heapanalytics.com/js/heap-" + e + ".js";
      var a = document.getElementsByTagName("script")[0];
      a.parentNode.insertBefore(r, a);
      for (var n = function (e) {
        return function () {
          heap.push([e].concat(Array.prototype.slice.call(arguments, 0)));
        };
      }, p = ["addEventProperties", "addUserProperties", "clearEventProperties", "identify", "resetIdentity", "removeEventProperty", "setEventProperties", "track", "unsetEventProperty"], o = 0; o < p.length; o++) heap[p[o]] = n(p[o]);
    };
    
    heap.load("YOUR_APP_ID");
    

    First, it defines a global heap object.

    Then it defines a heap.load method. heap.load first sets your app id e and options t to the window.heap object for future use.

    Next, it creates a new script element to load the heap.js tracking script. heap.js sends event data to Heap.

    After heap.js starts loading, heap.load stubs out the following methods so that they can be called before heap.js finishes loading:

    heap.addEventProperties
    heap.addUserProperties
    heap.clearEventProperties
    heap.identify
    heap.resetIdentity
    heap.removeEventProperty
    heap.setEventProperties
    heap.track
    heap.unsetEventProperty
    

    Lastly, the script calls the previously-defined heap.load with your app id. :)

    You can read more on Heap's developer documentation.