@snowplow/javascript-tracker
Version:
Web analytics for Snowplow
430 lines (375 loc) • 12.5 kB
HTML
<!DOCTYPE html>
<html>
<head>
<title>Integration test page</title>
</head>
<body style="width: 2000px; height: 2000px; position: relative">
<p id="title">Page for sending requests to Snowplow Micro</p>
<div id="init"></div>
<div id="secondInit"></div>
<div id="bottomRight" style="position: absolute; bottom: 0; right: 0">Bottom right</div>
<script>
var collector_endpoint = document.cookie.split('container=')[1].split(';')[0];
document.body.className += ' loaded';
</script>
<script>
function parseQuery() {
var query = {};
var pairs = window.location.search.substring(1).split('&');
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split('=');
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
}
return query;
}
</script>
<script>
(function (p, l, o, w, i, n, g) {
if (!p[i]) {
p.GlobalSnowplowNamespace = p.GlobalSnowplowNamespace || [];
p.GlobalSnowplowNamespace.push(i);
p[i] = function () {
(p[i].q = p[i].q || []).push(arguments);
};
p[i].q = p[i].q || [];
n = l.createElement(o);
g = l.getElementsByTagName(o)[0];
n.async = 1;
n.src = w;
g.parentNode.insertBefore(n, g);
}
})(window, document, 'script', './snowplow.js', 'snowplow');
(function (p, l, o, w, i, n, g) {
if (!p[i]) {
p.GlobalSnowplowNamespace = p.GlobalSnowplowNamespace || [];
p.GlobalSnowplowNamespace.push(i);
p[i] = function () {
(p[i].q = p[i].q || []).push(arguments);
};
p[i].q = p[i].q || [];
n = l.createElement(o);
g = l.getElementsByTagName(o)[0];
n.async = 1;
n.src = w;
g.parentNode.insertBefore(n, g);
}
})(window, document, 'script', './snowplow.js', 'secondSnowplow');
document.write(collector_endpoint);
var eventMethod = parseQuery().eventMethod;
window.snowplow('newTracker', 'sp', collector_endpoint, {
appId: 'sp-' + eventMethod,
platform: 'mob',
eventMethod: eventMethod,
contexts: {
webPage: true,
},
customHeaders: {
'Content-Language': 'de-DE, en-CA',
},
});
window.secondSnowplow('newTracker', 'b64off', collector_endpoint, {
appId: 'no-b64-' + eventMethod,
platform: 'web',
eventMethod: eventMethod,
encodeBase64: false,
contexts: {
webPage: false,
},
});
window.snowplow(function () {
document.getElementById('init').innerText = 'true';
});
window.secondSnowplow(function () {
document.getElementById('secondInit').innerText = 'true';
});
// Add a global context
var geolocationContext = {
schema: 'iglu:com.snowplowanalytics.snowplow/geolocation_context/jsonschema/1-1-0',
data: {
latitude: 40.0,
longitude: 55.1,
},
};
function eventTypeContextGenerator(args) {
var context = {};
context['schema'] = 'iglu:com.snowplowanalytics.snowplow/mobile_context/jsonschema/1-0-1';
context['data'] = {};
context['data']['osType'] = 'ubuntu';
context['data']['osVersion'] = '2018.04';
context['data']['deviceManufacturer'] = 'ASUS';
context['data']['deviceModel'] = String(args['eventType']);
return context;
}
// A filter that will only attach contexts to structured events
function structuredEventFilter(args) {
return args['eventType'] === 'se';
}
function erroneousContextGenerator(args) {
return {};
}
window.snowplow('enableGdprContext:sp', {
basisForProcessing: 'consent',
documentId: 'someId',
documentVersion: '0.1.0',
documentDescription: 'this document is a test',
});
window.snowplow('addGlobalContexts:sp', [erroneousContextGenerator]);
window.snowplow('addGlobalContexts:sp', [
[structuredEventFilter, [eventTypeContextGenerator, geolocationContext]],
]);
window.snowplow('setUserId:sp', 'Malcolm');
window.snowplow('enableActivityTracking:sp', { minimumVisitLength: 1, heartbeatDelay: 2 });
window.snowplow('trackPageView:sp', {
title: 'My Title',
context: [
{
schema: 'iglu:org.schema/WebPage/jsonschema/1-0-0',
data: {
keywords: ['tester'],
},
},
],
});
// Tracks to non-base64 encoding tracker
window.secondSnowplow('trackPageView', {
title: 'My Title',
context: [
// Set page title; add page context
{
schema: 'iglu:org.schema/WebPage/jsonschema/1-0-0',
data: {
keywords: ['tester'],
},
},
],
});
// This should have different pageViewId in web_page context
window.snowplow('trackPageView:sp');
window.snowplow('trackStructEvent:sp', {
category: 'Mixes',
action: 'Play',
label: 'MRC/fabric-0503-mix',
property: '',
value: 0.0,
});
window.snowplow('trackSelfDescribingEvent:sp', {
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0',
data: {
bannerId: 'ASO01043',
},
},
context: [],
timestamp: { type: 'ttm', value: 1477401868 },
});
window.snowplow('addTrans:sp', {
orderId: 'order-123',
total: 8000,
affiliation: 'acme',
tax: 100,
shipping: 50,
city: 'pheonix',
state: 'arizona',
country: 'USA',
currency: 'JPY',
});
// addItem might be called for each item in the shopping cart, or not at all.
window.snowplow('addItem:sp', {
orderId: 'order-123',
sku: '1001',
name: 'Blue t-shirt',
category: 'clothing',
price: '2000',
quantity: '2',
currency: 'JPY',
});
// trackTrans sends the transaction to Snowplow tracking servers.
// Must be called last to commit the transaction.
window.snowplow('trackTrans:sp');
var testAcceptRuleSet = {
accept: ['iglu:com.snowplowanalytics.snowplow/*/jsonschema/*-*-*'],
};
var testRejectRuleSet = {
reject: ['iglu:com.snowplowanalytics.snowplow/*/jsonschema/*-*-*'],
};
// test context rulesets
window.snowplow('addGlobalContexts:sp', [[testAcceptRuleSet, [eventTypeContextGenerator, geolocationContext]]]);
window.snowplow('trackSelfDescribingEvent:sp', {
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0',
data: {
bannerId: 'ASO01042',
},
},
timestamp: { type: 'ttm', value: 1477401869 },
});
window.snowplow('removeGlobalContexts:sp', [
[testAcceptRuleSet, [eventTypeContextGenerator, geolocationContext]],
]);
window.snowplow('addGlobalContexts:sp', [[testRejectRuleSet, [eventTypeContextGenerator, geolocationContext]]]);
window.snowplow('trackSelfDescribingEvent:sp', {
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/ad_impression/jsonschema/1-0-0',
data: {
bannerId: 'ASO01041',
},
},
timestamp: { type: 'ttm', value: 1477401868 },
});
window.snowplow('trackSocialInteraction:sp', {
action: 'retweet',
network: 'twitter',
target: '1234',
});
window.snowplow('trackAdImpression:sp', {
impressionId: '67965967893',
costModel: 'cpm',
cost: 5.5,
targetUrl: 'http://www.example.com',
bannerId: '23',
zoneId: '7',
advertiserId: '201',
campaignId: '12',
});
window.snowplow('trackAdClick:sp', {
targetUrl: 'http://www.example.com',
clickId: '12243253',
costModel: 'cpm',
cost: 2.5,
bannerId: '23',
zoneId: '7',
impressionId: '67965967893', // the same as in trackAdImpression
advertiserId: '201',
campaignId: '12',
});
window.snowplow('trackAdConversion:sp', {
conversionId: '743560297', // conversionId
costModel: 'cpa', // costModel
cost: 10, // cost
category: 'ecommerce', // category
action: 'purchase', // action
property: '', // property
initialValue: 99, // initialValue
advertiserId: '201', // advertiserId
campaignId: '12', // campaignId
});
window.snowplow('trackAddToCart:sp', {
sku: '000345',
name: 'blue tie',
category: 'clothing',
unitPrice: 3.49,
quantity: 2,
currency: 'GBP',
});
window.snowplow('trackRemoveFromCart:sp', {
sku: '000345',
name: 'blue tie',
category: 'clothing',
unitPrice: 3.49,
quantity: 1,
currency: 'GBP',
});
window.snowplow('trackSiteSearch:sp', {
terms: ['unified', 'log'],
filters: { category: 'books', 'sub-category': 'non-fiction' },
totalResults: 14,
pageResults: 8,
});
window.snowplow('trackTiming:sp', {
category: 'load',
variable: 'map_loaded',
timing: 50,
label: 'Map loading time',
});
window.snowplow('trackConsentGranted:sp', {
id: '1234',
version: '5',
name: 'consent_document',
description: 'a document granting consent',
expiry: '2020-11-21T08:00:00.000Z',
});
window.snowplow('trackConsentWithdrawn:sp', {
all: false,
id: '1234',
version: '5',
name: 'consent_document',
description: 'a document withdrawing consent',
});
try {
notExistentObject.notExistentProperty();
} catch (e) {
window.snowplow('trackError:sp', {
message: 'Cannot access notExistentProperty()',
filename: 'trackError.js',
lineno: 237,
colno: 5,
error: e,
});
}
window.snowplow('addEnhancedEcommerceActionContext:sp', {
id: 'T12345',
affiliation: 'Google Store - Online',
revenue: '37.39',
tax: '2.85',
shipping: '5.34',
coupon: 'WINTER2016',
});
window.snowplow('addEnhancedEcommerceImpressionContext:sp', {
id: 'P12345',
name: 'Android Warhol T-Shirt',
list: 'Search Results',
brand: 'Google',
category: 'Apparel/T-Shirts',
variant: 'Black',
position: '1',
});
window.snowplow('addEnhancedEcommerceProductContext:sp', {
id: 'P12345',
name: 'Android Warhol T-Shirt',
list: 'Search Results',
brand: 'Google',
category: 'Apparel/T-Shirts',
variant: 'Black',
quantity: 1,
});
window.snowplow('addEnhancedEcommercePromoContext:sp', {
id: 'PROMO_1234',
name: 'Summer Sale',
creative: 'summer_banner2',
position: 'banner_slot1',
});
window.snowplow('trackEnhancedEcommerceAction:sp', { action: 'purchase' });
// track unhandled exception
window.snowplow('enableErrorTracking:sp', {
filter: function (error) {
return true;
},
contextAdder: function (error) {
return [
{
schema: 'iglu:org.schema/WebPage/jsonschema/1-0-0',
data: {
keywords: ['tester'],
},
},
];
},
});
window.snowplow('setUserId:sp', 'Dave');
window.snowplow('setUserId:sp', null);
window.snowplow('trackStructEvent:sp', {
category: 'userIdTest1',
});
window.snowplow('setUserId:sp', 'Dave');
window.snowplow('setUserId:sp', undefined);
window.snowplow('trackStructEvent:sp', {
category: 'userIdTest2',
});
// Test for exception handling
function raiseException() {
notExistentObject.notExistentProperty();
}
setTimeout(raiseException, 3000);
</script>
</body>
</html>