Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Last active December 5, 2016 19:06
Show Gist options
  • Save sperand-io/32cff1a9003d117c6fae to your computer and use it in GitHub Desktop.
Save sperand-io/32cff1a9003d117c6fae to your computer and use it in GitHub Desktop.
What Is Segment Sending on My Behalf?

If you have node installed, you can clone the repo for whichever integration you'd like to use (say GA) then install segmentio-facade:

git clone https://github.com/segmentio/integration-google-analytics.git
cd integration-google-analytics
npm install segmentio-facade

Then create a new file in the top level of the directory with the script from this gist.

curl https://gist.githubusercontent.com/sperand-io/32cff1a9003d117c6fae/raw/a5932c51b68258475a0823db46b483cc7bbf1166/runner.js > runner.js

Follow the instructions in that script's comments to supply the call and settings from your project you'd like to simulate.

Finally, set your DEBUG env var and run that script.

DEBUG=* node runner

Boom, you'll get output showing the mapped request body and the endpoint(s) we're sending to.

/**
* Module dependencies
*/
var Integration = require('./lib');
var facade = require('segmentio-facade');
/**
* Your settings for the integration.
*/
var settings = /* paste the result of running the function below */
/**
* Go to your integrations page in chrome and run this in the console:
copy(segment.projectIntegrations.filter(function(obj) {
return obj.type === "<< ENTER INTEGRATION NAME HERE (Title Case)>>"
? true
: false;
})[0].settings
);
*/
/**
* Your event/identify/page etc.
* Copy directly from the debugger raw view!
*/
var event = /* example: {
"anonymousId": "32f28484-6c2d-4898-8e90-639aad371119",
"context": {
"library": {
"name": "analytics.js",
"version": "2.10.0"
},
"page": {
"path": "/simulator/",
"referrer": "",
"search": "",
"title": "Simulator - Segment",
"url": "https://tools.segment.io/simulator/"
},
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36",
"ip": "50.255.55.145"
},
"integrations": {
"Salesforce": true
},
"messageId": "c82f26af-66da-46ff-bd4e-c807291ad69f",
"receivedAt": "2015-07-28T19:36:19.245Z",
"sentAt": "2015-07-28T19:36:19.207Z",
"timestamp": "2015-07-28T19:36:19.239Z",
"traits": {
"Test_Field__c": "Gin & Juice",
"company": "Death Row",
"email": "snoop@dogg.com",
"name": "Snoop Dogg",
"phone": "270-33-4150",
"rating": 5,
"state": "California"
},
"type": "identify",
"userId": "UOENO123",
"originalTimestamp": "2015-07-28T19:36:19.201Z"
}; */
var Facades = {
track: facade.Track,
identify: facade.Identify,
alias: facade.Alias,
group: facade.Group,
page: facade.Page,
screen: facade.Screen
};
var message = new Facades[message.type](event);
var err = Integration.validate(message, settings);
if (err) return console.log(err);
var instance = new Integration(settings);
instance[message.type()](message, function(err, res){
if (err) return console.log('error: %s', err);
});
@bbeaird
Copy link

bbeaird commented Jun 14, 2016

cc @sperand-io ^^

@brennan
Copy link

brennan commented Jul 13, 2016

@sperand-io - Have the npm modules Segment is using changed slightly? I'm getting the following error when I run the runner.js file. It looks like this.redis() is no longer defined.

/Users/brennangamwell/dev/src/github.com/segmentio/integration-intercom/node_modules/segmentio-integration/lib/proto.js:180
  this.redis().set(key, 1, 'NX', 'PX', 15000, function(err, ok){
              ^

TypeError: Cannot read property 'set' of undefined

@bbeaird
Copy link

bbeaird commented Jul 14, 2016

FYI - Source settings can no longer be gotten via the methods above. However, you can get them from urls like this - https://segment.com/api/workspaces/segment-engineering/projects/testing/integration-settings - and search for the integration and copy the settings object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment