2

我正在从 SegmentIO 切换到他们的开源版本。我将此博客文章用作指南,但它没有跟踪。任何人都看到此代码的问题:

  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 (method) {
    return function () {
      var args = Array.prototype.slice.call(arguments);
      args.unshift(method);
      window.analytics.push(args);
      return 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);
}

analytics.load = function(callback) {
  var script = document.createElement('script');
  script.async = true;
  script.type = 'text/javascript';
 script.src = 'https://assets.gonegreenstore.com.s3.amazonaws.com/js/analytics.min.js';
  script.addEventListener('load', function (e) {
    if(typeof callback === 'function') {
      callback(e);
    }
  }, false);
  var firstScript = document.getElementsByTagName('script')[0];
  firstScript.parentNode.insertBefore(script, firstScript);
};

// Continues from above
analytics.load(function () {
  analytics.initialize({
    'Google Analytics': {
       trackingId: 'UA-40148609-3'
     }        
  });
});

analytics.page();
4

2 回答 2

3

博客中的 OK 代码要么错误,要么已过时。事实证明,您无法真正加载开箱即用的开源版本异步。我已经修改了代码以使其工作。

要点:https ://gist.github.com/cyberwombat/11008970

// Create a dummy analytics object until real loaded
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(method) {
  return function() {
    var args = Array.prototype.slice.call(arguments);
    args.unshift(method);
    window.analytics.push(args);
    return 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);
}

// Load analytics async
analytics.load = function(callback) {
  if (document.getElementById('analytics-js')) return;

  // We make a copy if our dummy object
  window.a = window.analytics;
  var script = document.createElement('script');
  script.async = true;
  script.id = 'analytics-js';
  script.type = 'text/javascript';
  script.src = ('https:' === document.location.protocol ? 'https://' : 'http://') + 'path/to/your/analytics.min.js';
  script.addEventListener('load', function(e) {
    if (typeof callback === 'function') {
      callback(e);
    }
  }, false);
  var first = document.getElementsByTagName('script')[0];
  first.parentNode.insertBefore(script, first);
};

analytics.load(function() {

  // On load init our integrations
  analytics.initialize({
    'Google Analytics': {
      trackingId: 'UA-XXXXXX-1'
    }
  });  
  // Now copy whatever we applied to our dummy object to the real analytics
  while (window.a.length > 0) {
    var item = window.a.shift();
    var method = item.shift();
    if (analytics[method]) analytics[method].apply(analytics, item);
  }
});

analytics.page();
于 2014-04-17T20:16:49.057 回答
1

你可以像这样使用它:

npm install --save analytics.js

import analytics from 'analytics.js';

analytics.initialize({
    'Google Analytics': {
        trackingId: 'UA-XXXXXX-1',
    }
});
analytics.page();
于 2016-06-07T16:47:50.287 回答