6

How or what do I have to do to send all the analytic data to my own server when using segment analytics javascript library?

https://segment.com/docs/libraries/analytics.js/

cool breeze
  • 4,461
  • 5
  • 38
  • 67

2 Answers2

2

Two possible options off the top of my head.

  1. You could leverage Segment 'as is' and push data into your servers via the webhook integration: https://segment.com/docs/integrations/webhooks/

  2. As Segment's Javascript library is open source, you could manually deploy it on your site. This would involve making some changes to the endpoint the app sends data to: https://github.com/segmentio/analytics.js

ccnixon
  • 267
  • 1
  • 11
  • I tried self-hosting analytics.js and then following the quickstart tutorial to load the jscript asynchronously and initialize it. However, it still doesnt send any events. I can see the analytics object initialized in the console, i.e made sure that I dont have a stub version that just queues to the internal queue. – feroze Feb 20 '18 at 00:31
  • 1
    Is there example solution for this question? How can I send my data for example to localhost? I'm looking for where source code of analytics.js send the data but I cant find it. – Anıl Sevici Jun 18 '18 at 15:35
2

Here is the my solution.Just change //api.segment.io/v1 with your server.This below code belongs to https://github.com/segmentio/analytics.js/blob/2.11.0/analytics.js

/**
 * Expose `Segment` integration.
 */

var Segment = exports = module.exports = integration('Segment.io')
  .option('apiKey', '');

  Segment.prototype.send = function(path, msg, fn) {
  var url = scheme() + '//api.segment.io/v1' + path;
  var headers = { 'Content-Type': 'text/plain' };
  fn = fn || noop;
  var self = this;

  // msg
  msg = this.normalize(msg);

  // send
  send(url, msg, headers, function(err, res) {
    self.debug('sent %O, received %O', msg, arguments);
    if (err) return fn(err);
    res.url = url;
    fn(null, res);
  });
};

Then initialize analytics with Segment.io.Btw if you want change name of Segment.io replace name of integration('Segment.io') whatever you want.

analytics.initialize({
    'Segment.io': {
        apiKey: ''
    }
});
Anıl Sevici
  • 214
  • 2
  • 12