Skip to content

Commit edda123

Browse files
committed
Changed trackUnstructEvent to take a JSON containing schema and data fields (snowplow#197)
Added hardcoded schema to unstructured events (snowplow#196)
1 parent 6e33887 commit edda123

File tree

4 files changed

+92
-70
lines changed

4 files changed

+92
-70
lines changed

CHANGELOG

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ Added pageUnloadTimer option to argmap (#171)
3535
Added event vendor as argument to trackUnstructEvent (#157)
3636
Added custom context vendor configuration option (#180)
3737
Removed type hints from unstructured events and custom contexts (#163)
38-
Added hardcoded header to custom context arrays (#199)
38+
Added hardcoded schema to custom context arrays (#199)
39+
Added hardcoded schema to unstructured events (#196)
40+
Changed trackUnstructEvent to take a JSON containing schema and data fields (#197)
3941

4042
Version 1.0.3 (2014-06-27)
4143
--------------------------

examples/web/async-medium.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
// window.new_name_here('trackPageView', 'Async Test'); // Track the page view with custom title
5252
window.new_name_here('trackPageView', null , [ // Auto-set page title; add page context
5353
{
54-
schema: "com.example_company/page/1.2.1",
54+
schema: "com.example_company/page/json/1-2-1",
5555
data: {
5656
pageType: 'test',
5757
lastUpdated: new Date(2014,1,26)
5858
}
5959
},
6060
{
61-
schema: "com.example_company/user/2.0.0",
61+
schema: "com.example_company/user/json/2-0-0",
6262
data: {
6363
userType: 'tester',
6464
}
@@ -87,16 +87,18 @@
8787
alert("Viewing a product");
8888

8989
// Only the "uri" tracker will track the Viewed Product event
90-
window.new_name_here('com.acme_company', 'trackUnstructEvent:uri', 'Viewed Product',
91-
{
90+
window.new_name_here('trackUnstructEvent:uri', {
91+
schema: 'com.acme_company/viewed_product/json/5.0.0',
92+
data: {
9293
productId: 'ASO01043',
9394
category: 'Dresses',
9495
brand: 'ACME',
9596
returning: true,
9697
price: 49.95,
9798
sizes: ['xs', 's', 'l', 'xl', 'xxl'],
9899
availableSince: new Date(2013,3,7)
99-
});
100+
}
101+
});
100102
}
101103

102104
function addEcommerceTransaction() {
@@ -130,7 +132,7 @@
130132
'2', // quantity - required
131133
'JPY', // currency
132134
[{ // context
133-
schema: "com.example_company/products/1.0.0",
135+
schema: "com.example_company/products/json/1-0-0",
134136
data: {
135137
"launchDate": new Date(2013,3,7)
136138
}

examples/web/async-small.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
// window.snowplow('trackPageView', 'Async Test'); // Track the page view with custom title
4141
window.snowplow('trackPageView', null , [ // Auto-set page title; add page context
4242
{
43-
schema: "com.example_company/page/1.2.1",
43+
schema: "com.example_company/page/json/1-2-1",
4444
data: {
4545
pageType: 'test',
4646
lastUpdated: new Date(2014,1,26)
4747
}
4848
},
4949
{
50-
schema: "com.example_company/user/2.0.0",
50+
schema: "com.example_company/user/json/2-0-0",
5151
data: {
5252
userType: 'tester',
5353
}
@@ -70,16 +70,18 @@
7070
}
7171
function viewProduct() {
7272
alert("Viewing a product");
73-
window.snowplow('trackUnstructEvent', 'com.example_company', 'Viewed Product',
74-
{
73+
window.snowplow('trackUnstructEvent', {
74+
schema: 'com.acme_company/viewed_product/json/5.0.0',
75+
data: {
7576
productId: 'ASO01043',
7677
category: 'Dresses',
7778
brand: 'ACME',
7879
returning: true,
7980
price: 49.95,
8081
sizes: ['xs', 's', 'l', 'xl', 'xxl'],
8182
availableSince: new Date(2013,3,7)
82-
});
83+
}
84+
});
8385
}
8486

8587
function addEcommerceTransaction() {
@@ -111,7 +113,7 @@
111113
'2', // quantity - required
112114
'JPY', // currency
113115
[{ // context
114-
schema: "com.example_company/products/1.0.0",
116+
schema: "com.example_company/products/json/1-0-0",
115117
data: {
116118
"launchDate": new Date(2013,3,7)
117119
}

src/js/tracker.js

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@
9797
configDefaultVendor = 'com.snowplowanalytics',
9898

9999
// The schema against which custom context arrays should be validated
100-
configContextSchema = "com.snowplowanalytics/contexts/1.0.0",
100+
configContextSchema = 'com.snowplowanalytics/contexts/json/1-0-0',
101+
102+
// The schema against which unstructured event envelopes should be validated
103+
configUnstructEventSchema = 'com.snowplowanalytics/unstruct_event/json/1-0-0',
101104

102105
// Platform defaults to web for this tracker
103106
configPlatform = argmap.hasOwnProperty('platform') ? argmap.platform : 'web',
@@ -673,20 +676,23 @@
673676
/**
674677
* Log an unstructured event happening on this page
675678
*
676-
* @param string name The name of the event
677-
* @param object properties The properties of the event
678-
* @param string eventVendor The author of the event
679+
* @param object eventJson Contains the properties and schema location for the event
679680
* @param object context Custom context relating to the event
680681
*/
681-
function logUnstructEvent(eventVendor, name, properties, context) {
682-
var sb = payload.payloadBuilder(configEncodeBase64);
683-
sb.add('e', 'ue'); // 'ue' for Unstructured Event
684-
sb.add('ue_na', name);
685-
sb.addJson('ue_px', 'ue_pr', properties);
686-
sb.addJson('cx', 'co', completeContext(context));
687-
sb.add('evn', eventVendor);
688-
var request = getRequest(sb);
689-
sendRequest(request, configTrackerPause);
682+
function logUnstructEvent(eventJson, context) {
683+
helpers.deleteEmptyProperties(eventJson.data);
684+
if (!lodash.isEmpty(eventJson.data)) {
685+
var envelope = {
686+
schema: configUnstructEventSchema,
687+
data: eventJson
688+
},
689+
sb = payload.payloadBuilder(configEncodeBase64);
690+
sb.add('e', 'ue'); // 'ue' for Unstructured Event
691+
sb.addJson('ue_px', 'ue_pr', envelope);
692+
sb.addJson('cx', 'co', completeContext(context));
693+
var request = getRequest(sb);
694+
sendRequest(request, configTrackerPause);
695+
}
690696
}
691697

692698
/**
@@ -768,14 +774,17 @@
768774
// TODO: this functionality is not yet fully implemented.
769775
// See https://github.com/snowplow/snowplow/issues/75
770776
function logLink(targetUrl, elementId, elementClasses, elementTarget, context) {
771-
var linkClickJson = {
772-
targetUrl: targetUrl,
773-
elementId: elementId,
774-
elementClasses: elementClasses,
775-
elementTarget: elementTarget
777+
var eventJson = {
778+
schema: configDefaultVendor + '/link_click/json/1-0-0',
779+
data: {
780+
targetUrl: targetUrl,
781+
elementId: elementId,
782+
elementClasses: elementClasses,
783+
elementTarget: elementTarget
784+
},
776785
};
777786

778-
logUnstructEvent(configDefaultVendor, 'link_click', helpers.deleteEmptyProperties(linkClickJson), context);
787+
logUnstructEvent(eventJson, context);
779788
}
780789

781790
/**
@@ -1355,13 +1364,11 @@
13551364
/**
13561365
* Track an unstructured event happening on this page.
13571366
*
1358-
* @param string eventVendor The author of the event
1359-
* @param string name The name of the event
1360-
* @param object properties The properties of the event
1361-
* @param object Custom context relating to the event
1367+
* @param object eventJson Contains the properties and schema location for the event
1368+
* @param object context Custom context relating to the event
13621369
*/
1363-
trackUnstructEvent: function (eventVendor, name, properties, context) {
1364-
logUnstructEvent(eventVendor, name, properties, context);
1370+
trackUnstructEvent: function (eventJson, context) {
1371+
logUnstructEvent(eventJson, context);
13651372
},
13661373

13671374
/**
@@ -1504,18 +1511,21 @@
15041511
*/
15051512
trackAdImpression: function(impressionId, costModel, cost, targetUrl, bannerId, zoneId, advertiserId, campaignId, context) {
15061513
trackCallback(function () {
1507-
var adImpressionJson = {
1508-
impressionId: impressionId,
1509-
costModel: costModel,
1510-
cost: cost,
1511-
bannerId: bannerId,
1512-
targetUrl: targetUrl,
1513-
zoneId: zoneId,
1514-
advertiserId: advertiserId,
1515-
campaignId: campaignId
1514+
var eventJson = {
1515+
schema: configDefaultVendor + '/ad_impression/json/1-0-0',
1516+
data: {
1517+
impressionId: impressionId,
1518+
costModel: costModel,
1519+
cost: cost,
1520+
bannerId: bannerId,
1521+
targetUrl: targetUrl,
1522+
zoneId: zoneId,
1523+
advertiserId: advertiserId,
1524+
campaignId: campaignId
1525+
}
15161526
};
15171527

1518-
logUnstructEvent(configDefaultVendor, 'ad_impression', helpers.deleteEmptyProperties(adImpressionJson), context);
1528+
logUnstructEvent(eventJson, context);
15191529
});
15201530
},
15211531

@@ -1534,19 +1544,22 @@
15341544
* @param object Custom context relating to the event
15351545
*/
15361546
trackAdClick: function(targetUrl, clickId, costModel, cost, bannerId, zoneId, impressionId, advertiserId, campaignId, context) {
1537-
var adClickJson = {
1538-
targetUrl: targetUrl,
1539-
clickId: clickId,
1540-
costModel: costModel,
1541-
cost: cost,
1542-
bannerId: bannerId,
1543-
zoneId: zoneId,
1544-
impressionId: impressionId,
1545-
advertiserId: advertiserId,
1546-
campaignId: campaignId
1547+
var eventJson = {
1548+
schema: configDefaultVendor + '/ad_click/json/1-0-0',
1549+
data: {
1550+
targetUrl: targetUrl,
1551+
clickId: clickId,
1552+
costModel: costModel,
1553+
cost: cost,
1554+
bannerId: bannerId,
1555+
zoneId: zoneId,
1556+
impressionId: impressionId,
1557+
advertiserId: advertiserId,
1558+
campaignId: campaignId
1559+
}
15471560
};
15481561

1549-
logUnstructEvent(configDefaultVendor, 'ad_click', helpers.deleteEmptyProperties(adClickJson), context);
1562+
logUnstructEvent(eventJson, context);
15501563
},
15511564

15521565
/**
@@ -1564,19 +1577,22 @@
15641577
* @param object Custom context relating to the event
15651578
*/
15661579
trackAdConversion: function(conversionId, costModel, cost, category, action, property, initialValue, advertiserId, campaignId, context) {
1567-
var adConversionJson = {
1568-
conversionId: conversionId,
1569-
costModel: costModel,
1570-
cost: cost,
1571-
category: category,
1572-
action: action,
1573-
property: property,
1574-
initialValue: initialValue,
1575-
advertiserId: advertiserId,
1576-
campaignId: campaignId
1580+
var eventJson = {
1581+
schema: configDefaultVendor + '/ad_conversion/json/1-0-0',
1582+
data: {
1583+
conversionId: conversionId,
1584+
costModel: costModel,
1585+
cost: cost,
1586+
category: category,
1587+
action: action,
1588+
property: property,
1589+
initialValue: initialValue,
1590+
advertiserId: advertiserId,
1591+
campaignId: campaignId
1592+
}
15771593
};
15781594

1579-
logUnstructEvent(configDefaultVendor, 'ad_conversion', helpers.deleteEmptyProperties(adConversionJson), context);
1595+
logUnstructEvent(eventJson, context);
15801596
}
15811597
}
15821598
}

0 commit comments

Comments
 (0)