Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Last active April 1, 2019 18:36
Show Gist options
  • Save sperand-io/2123a1eed69eb8ed6990 to your computer and use it in GitHub Desktop.
Save sperand-io/2123a1eed69eb8ed6990 to your computer and use it in GitHub Desktop.
Piggybacking Segment Events for Ad-Hoc Conversion Pixels
/**
* Pixel Loader.
* It will make the http request on construction, no need to dom insert.
*/
const loadPixel = (src, fn=()=>{}) => ({
...new Image,
onload() { fn() },
height: 1,
width: 1,
src
});
/**
* Set up a listener for `.track()` calls with a callback
* that takes the event name and properties and download
* arbitrary pixels based on that name and properties :)
*/
analytics.on('track', function(event, { id = '', revenue = '' }) {
switch (event) {
case 'Conversion Event':
loadPixel(`//1pixel.com/${event}/stuff?id=${id}&revenue=${revenue}`);
loadPixel(`//2pixel.com/${event}/stuff?id=${id}&revenue=${revenue}&param=param`);
break;
case 'Other Conversion Event':
loadPixel(`//1pixel.com/${event}/stuff?id=${id}&revenue=${revenue}`);
loadPixel(`//2pixel.com/${event}/stuff?id=${id}&revenue=${revenue}&param=param`);
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment