Last active
April 1, 2019 18:36
-
-
Save sperand-io/2123a1eed69eb8ed6990 to your computer and use it in GitHub Desktop.
Piggybacking Segment Events for Ad-Hoc Conversion Pixels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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}¶m=param`); | |
break; | |
case 'Other Conversion Event': | |
loadPixel(`//1pixel.com/${event}/stuff?id=${id}&revenue=${revenue}`); | |
loadPixel(`//2pixel.com/${event}/stuff?id=${id}&revenue=${revenue}¶m=param`); | |
break; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment