@financial-times/o-tracking
Version:
Provides tracking for a product. Tracking requests are sent to the Spoor API.
30 lines (26 loc) • 774 B
JavaScript
import {encode} from '../../utils.js';
/**
* Image based transport mechanism
*
* @returns {object} - Object with three properties: name, send and complete
*/
function image () {
const image = new Image(1,1);
return {
name: 'image',
send: function (url, data) {
url = url.replace('https://spoor-api.ft.com/ingest', 'https://spoor-api.ft.com/px.gif');
image.src = url + (url.indexOf('?') > -1 ? '&' : '?') + 'data=' + encode(data);
},
complete: function (callback) {
if (image.addEventListener) {
image.addEventListener('error', callback);
image.addEventListener('load', () => callback());
} else { // it's IE!
image.attachEvent('onerror', callback);
image.attachEvent('onload', () => callback());
}
}
};
}
export { image };