2

I'm trying migrate website tracking from old Google Analytics script ga.js to new analytics.js.

Currently I have enabled "local remote server mode" to send tracing requests to my server as well. Following code was used to enable this mode on ga.js:

_gaq.push(['_setLocalRemoteServerMode']); (more info).

But I can not find how to enable this feature on new analytics.js. Has anybody solved this already?

  • Are you still intending on using Urchin in conjunction with GA Universal? – dan Jan 22 '14 at 8:10
  • I'll use anything what will allow send GA requests to my server too. – Pawka Jan 22 '14 at 8:58
  • 1
    Not sure I follow that completely - the above is for Urchin Software. You can add other third-party tracking code to your pages, if that's what you meant. However, it doesn't appear that Universal supports Urchin (at least there's no reference to it anymore). – dan Jan 22 '14 at 9:13
  • I've implemented by extending ga object, extracting data and sending requests to my server manually. – Pawka Jan 20 '15 at 7:23
3

There doesn't seem to be an easy function built into analytics.js that I have found, however, there is this code for sending the same data via an XHR request to your server.

ga('create', 'UA-XXXXX-Y', 'auto');

ga(function(tracker) {

  // Grab a reference to the default sendHitTask function.
  var originalSendHitTask = tracker.get('sendHitTask');

  // Modifies sendHitTask to send a copy of the request to a local server after
  // sending the normal request to www.google-analytics.com/collect.
  tracker.set('sendHitTask', function(model) {
    originalSendHitTask(model);
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/localhits', true);
    xhr.send(model.get('hitPayload'));
  });
});

ga('send', 'pageview');

https://developers.google.com/analytics/devguides/collection/analyticsjs/tasks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.