Skip to content

Instantly share code, notes, and snippets.

@jordanskole
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanskole/12eff217c0b65a5abe70 to your computer and use it in GitHub Desktop.
Save jordanskole/12eff217c0b65a5abe70 to your computer and use it in GitHub Desktop.
Segment Tracking Pixel (for email)
'use strict';
/*
* Build a tracking pixel for segment-dot-i-o,
* to use places where javascript wont go,
* in the rain, the sleet, the ice and the snow,
* or just in email, ya-know?
*
* https://segment.com/docs/api/tracking/http/#tracking-pixels
*/
var express = require('express'),
app = express();
// var Analytics = require('analytics-node'),
// analytics = new Analytics(process.env.SEGMENT_WRITE_KEY);
var shortid = require('shortid'); // do we need this?
app.get('/', function (req, res) {
res.send('System is ready...');
});
// image options for responding w the pixel
var options = {
root: __dirname,
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
}
// Analytics Track Event
app.get('/track/:event/:id?', function (req, res) { // event is required, id is not
console.log('event: ' + req.params.event +', id: ' + req.params.id);
console.log('properties: ' + req.query);
// build the message
var message = {
event: req.params.event,
properties: req.query
};
// "You must include either a userId or anonymousId with every API call. ~Segment"
if (typeof req.params.id !== 'undefined') {
messagte.id = req.params.id;
} else {
message.anonymousId = shortid.generate(); // is this necessary?
}
// place async api call to segment here
// analytics.track(message);
// 1x1 tracking pixel
res.sendFile('/images/1.png', options, function (error) {
if (error) {
console.log('error: ' + error);
}
});
});
// Analytics Identify
app.get('/identify/:id?', function (req, res) { // "You must include either a userId or anonymousId with every API call. ~Segment"
console.log('id:' + req.params.id);
console.log('traits' + req.query);
var message = {
userId: req.params.id,
traits: req.query
};
// "You must include either a userId or anonymousId with every API call. ~Segment"
// but why identify at all if that's the case?
// if (typeof req.params.id !== 'undefined') {
// messagte.id = req.params.id;
// } else {
// message.anonymousId = shortid.generate(); // is this necessary?
// }
// place async api call to segment here
// analytics.identify(message);
// 1x1 tracking pixel
res.sendFile('/images/1.png', options, function (error) {
if (error) {
console.log('error: ' + error);
}
});
});
// do the server
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
// console.log('Example app listening at http://%s:%s', host, port);
});
@jordanskole
Copy link
Author

Not completely sure about passing ID - especially because userID won't be available in other services than our own.

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