-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.tpl
462 lines (424 loc) · 17.7 KB
/
template.tpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
___TERMS_OF_SERVICE___
By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
https://developers.google.com/tag-manager/gallery-tos (or such other URL as
Google may provide), as modified from time to time.
___INFO___
{
"type": "TAG",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Twenga Pixel",
"categories": ["CONVERSIONS","AFFILIATE_MARKETING"],
"brand": {
"id": "twenga",
"displayName": "Twenga",
"thumbnail": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEwAAABMCAIAAABI9cZ8AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAZFJREFUeNrs279LQlEUwHFfvtCWjJaGwBpqjJagRZqiP6KoraZoa3ON/gAb3YKWNiMHoSVoCBs0CcEanknQWyJbMpLogFs/zPPo5uX1PbxBrle5H+713PN4Vye2m4+EPQYi/yBAggQJEiRIkCBBggRpUbjaDywkRzfnJmfHhicSQ0ZHVm8+y/Wh8bH1unfhnd4+qL7KUd1PplPT6dRU32dmv3K3fnxpZLnKHNoglFidGZfLCFJWqT0/M9VgFMiR+KA9SEkKZNdQID8n3t9BHl379iBVu4gCmSl6Zf/JBqEMY/uk2nv/qLu40nvvbKkhW6uUAf1KQlIMHFbv13JleWGqGAgQsrsWlue7dFg6OO+sPUmYiZirrYGMlHVGFyHZFSRIkCBBggQJEiRIkCBBggQJEiRIkCBBggQJEiRIkCC7x48nGII9PLZuJjNF77u3cjU/JMids5svH5VL40a+8gcD0J3+CBat9lu21Gi+tONutHOAVHjSslW4Uh3iCBwOfy0ECRIkSJAgQYIECdKeeBdgAMdqddTmfQP5AAAAAElFTkSuQmCC"
},
"description": "Twenga Pixel GTM tag template",
"containerContexts": [
"WEB"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "masterSiteId",
"displayName": "masterSiteId",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "NUMBER"
}
]
},
{
"type": "TEXT",
"name": "host",
"displayName": "host",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
},
{
"type": "REGEX",
"args": [
"^https:\\/\\/.*\\..*\\.[a-z]*$"
]
}
]
},
{
"type": "SELECT",
"name": "event",
"displayName": "event",
"macrosInSelect": true,
"selectItems": [
{
"value": "tx",
"displayValue": "Transaction"
},
{
"value": "pp",
"displayValue": "Product"
}
],
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "SELECT",
"name": "currency",
"displayName": "currency",
"macrosInSelect": true,
"selectItems": [
{
"value": "EUR",
"displayValue": "EUR"
},
{
"value": "GBP",
"displayValue": "GBP"
},
{
"value": "PLN",
"displayValue": "PLN"
}
],
"simpleValueType": true
},
{
"type": "TEXT",
"name": "order_id",
"displayName": "DataLayer Order Id",
"simpleValueType": true,
"help": "Datalayer variable containing the order id, for transaction events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "order_items",
"displayName": "DataLayer Items",
"simpleValueType": true,
"help": "Datalayer variable containing the order items list, for transaction events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "order_attribution_weight",
"displayName": "DataLayer order attribution weight",
"simpleValueType": true,
"help": "Datalayer variable containing the order attribution weight , for transaction events. The order attribution weight is optional."
},
{
"type": "TEXT",
"name": "item_ref_id_key",
"displayName": "DataLayer item ref id key",
"simpleValueType": true,
"help": "Key within the items list that contains the item ref id , for transaction events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "item_price_key",
"displayName": "DataLayer item price key",
"simpleValueType": true,
"help": "Key within the items list that contains the item price, for transaction events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "item_quantity_key",
"displayName": "DataLayer item quantity key",
"simpleValueType": true,
"help": "Key within the items list that contains the item quantity , for transaction events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "item_variant_id_key",
"displayName": "DataLayer item variant id key",
"simpleValueType": true,
"help": "Key within the items list that contains the item variant id, for transaction events. The variant id is optional."
},
{
"type": "TEXT",
"name": "product_price",
"displayName": "DataLayer product price",
"simpleValueType": true,
"help": "Datalayer variable containing the product price, for product display events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "product_ref_id",
"displayName": "DataLayer product ref id",
"simpleValueType": true,
"help": "Datalayer variable containing the product ref, for product display events.",
"valueValidators": [
{
"type": "NON_EMPTY"
}
]
},
{
"type": "TEXT",
"name": "product_variant_id",
"displayName": "DataLayer product variant id",
"simpleValueType": true,
"help": "Datalayer variable containing the product variant id, for product display events. The product variant id is optional."
},
{
"type": "TEXT",
"name": "country",
"displayName": "Datalayer country code",
"simpleValueType": true,
"help": "Datalayer variable containing the country code for both Product and Transaction events. The country code is optional.",
"canBeEmptyString": true
}
]
___SANDBOXED_JS_FOR_WEB_TEMPLATE___
const sendPixel = require('sendPixel');
const queryPermission = require('queryPermission');
var TwgT = {
sendRequest : function() {
if (!data.host || !data.event) {
data.gtmOnFailure();return false;
}
var oTwgTConfig = {"master_site_id":data.masterSiteId};
var sFileName = '';
var sTwgTConfig = '';
const sHost = data.host;
const sEvent = data.event;
//Transaction items keys
const itemrefidKey = data.item_ref_id_key;
const itemquantityKey = data.item_quantity_key;
const itemvariantidKey = data.item_variant_id_key;
const itempriceKey = data.item_price_key;
if (data.currency) {
oTwgTConfig.currency = data.currency;
} else {
oTwgTConfig.currency = 'EUR';
}
//Country code
if (data.country) {
oTwgTConfig.country = data.country;
} else {
oTwgTConfig.country = undefined;
}
//Transaction case
if (sEvent == "tx") {
//Retrieving transaction data from dataLayer
oTwgTConfig.order_id = data.order_id;
if (!oTwgTConfig.order_id) {
oTwgTConfig.order_id = undefined;
}
oTwgTConfig.items = data.order_items;
if (!oTwgTConfig.items) {
oTwgTConfig.items = [];
}
oTwgTConfig.attribution_weight = data.order_attribution_weight;
if (!oTwgTConfig.attribution_weight) {
oTwgTConfig.attribution_weight = undefined;
}
//Building Json string
sTwgTConfig = '{"m":"'+oTwgTConfig.master_site_id+'","o":"'+oTwgTConfig.order_id+'","c":"'+oTwgTConfig.currency+'","cn":"'+oTwgTConfig.country+'","w":"'+oTwgTConfig.attribution_weight+'","is":[';
var aItems = [];
for (var index in oTwgTConfig.items) {
var sItemConfig = '';
sItemConfig = '{"i":"'+oTwgTConfig.items[index][itemrefidKey]+'","q":"'+oTwgTConfig.items[index][itemquantityKey]+'"';
if (oTwgTConfig.items[index][itemvariantidKey]) {
sItemConfig += ',"v":"'+oTwgTConfig.items[index][itemvariantidKey]+'"';
}
sItemConfig += ',"p":"'+oTwgTConfig.items[index][itempriceKey]+'"}';
aItems.push(sItemConfig);
}
sTwgTConfig += aItems.join(',')+']}';
}
//Product case
else if (sEvent == "pp") {
//Retrieving product data from dataLayer
oTwgTConfig.price = data.product_price;
if (!oTwgTConfig.price) {
oTwgTConfig.price = undefined;
}
oTwgTConfig.ref_id = data.product_ref_id;
if (!oTwgTConfig.ref_id) {
oTwgTConfig.ref_id = undefined;
}
oTwgTConfig.variant_id = data.product_variant_id;
if (!oTwgTConfig.variant_id) {
oTwgTConfig.variant_id = undefined;
}
//Building Json string
sTwgTConfig = '{"m":"'+oTwgTConfig.master_site_id+'","c":"'+oTwgTConfig.currency+'","cn":"'+oTwgTConfig.country+'","i":"'+oTwgTConfig.ref_id+'"';
if (oTwgTConfig.variant_id) {
sTwgTConfig += ',"v":"'+oTwgTConfig.variant_id+'"';
}
sTwgTConfig += ',"p":"'+oTwgTConfig.price+'"}';
} else {
data.gtmOnFailure();
return false;
}
//Building tracking URL
sFileName = sEvent+'_'+sTwgTConfig+'.png';
var url = sHost+'/t/gtm/'+sFileName;
if (queryPermission('send_pixel', url)) {
var bResult = sendPixel(url, function(){data.gtmOnSuccess();return true;}, function(){data.gtmOnFailure();return false;});
}
data.gtmOnFailure();
}
};
TwgT.sendRequest();
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "send_pixel",
"versionId": "1"
},
"param": [
{
"key": "allowedUrls",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
___TESTS___
scenarios:
- name: Failure No Event
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: undefined, \n host:'https://twenga.twgdns.com',\
\ \n masterSiteId:666,\n order_id:123,\n order_items:\"[{'price':10.00,'id':666666,'quantity':1}]\"\
,\n order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished with failure.\nassertApi('gtmOnFailure').wasCalled();"
- name: Failure - No host
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: 'pp', \n host:'', \n masterSiteId:666,\n order_id:123,\n\
\ order_items:\"[{'price':10.00,'id':666666,'quantity':1}]\",\n order_attribution_weight:1,\n\
\ item_ref_id_key:'id',\n item_price_key:'price',\n item_quantity_key:'quantity',\n\
\ item_variant_id:0,\n product_price:0.25,\n product_ref_id:123,\n product_variant_id:0\n\
});\n\n// Verify that the tag finished with failure.\nassertApi('gtmOnFailure').wasCalled();"
- name: Failure Send Pixel error
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onFailure();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: 'pp', \n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n\
\ order_id:123,\n order_items:\"[{'price':10.00,'id':666666,'quantity':1}]\"\
,\n order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished with failure.\nassertApi('gtmOnFailure').wasCalled();"
- name: Product - Success
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: 'pp', \n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n\
\ order_id:123,\n order_items:\"[{'price':10.00,'id':666666,'quantity':1}]\"\
,\n order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0,\n country:'FR'\n});\n\n// Verify\
\ that the tag finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Product - Success - Minimum params
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: 'pp', \n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n\
\ order_id:undefined,\n order_items:undefined,\n order_attribution_weight:undefined,\n\
\ item_ref_id_key:'id',\n item_price_key:'price',\n item_quantity_key:'quantity',\n\
\ item_variant_id:undefined,\n product_price:undefined,\n product_ref_id:undefined,\n\
\ product_variant_id:undefined,\n country:undefined\n});\n\n// Verify that the\
\ tag finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Transaction - Success
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\n// Call runCode to run the template's\
\ code.\nrunCode({\n event: 'tx', \n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n\
\ order_id:123,\n order_items:[{'price':10.00,'id':666666,'quantity':1,'variant_id':'123ABC'},{'price':15.99,'id':784469,'quantity':8,'variant_id':'123ABC'},{'price':15048.43,'id':13548,'quantity':2,'variant_id':'123ABC'}],\n\
\ order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Transaction - Success - Missing order_id
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\nmock('copyFromDataLayer', function(DLVar){\n\
\ if (DLVar == 'twenga_order_id') {return undefined;}\n else if (DLVar == 'twenga_currency')\
\ {return 'EUR';}\n else if (DLVar == 'twenga_items') {return [{'price':10.00,'id':666666,'quantity':1}];}\n\
\ else{return false;}\n});\n\n// Call runCode to run the template's code.\nrunCode({\n\
\ event: 'tx', \n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n\
\ order_id:undefined,\n order_items:[{'price':10.00,'id':666666,'quantity':1,'variant_id':'123ABC'},{'price':15.99,'id':784469,'quantity':8,'variant_id':'123ABC'},{'price':15048.43,'id':13548,'quantity':2,'variant_id':'123ABC'}],\n\
\ order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished with failure.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Transaction - Success - Missing Currency (Defaults to EUR)
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\nrunCode({\n event: 'tx', \n currency:\
\ undefined,\n host:'https://twenga.twgdns.com', \n masterSiteId:666,\n order_id:123,\n\
\ order_items:[{'price':10.00,'id':666666,'quantity':1,'variant_id':'123ABC'},{'price':15.99,'id':784469,'quantity':8,'variant_id':'123ABC'},{'price':15048.43,'id':13548,'quantity':2,'variant_id':'123ABC'}],\n\
\ order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Transaction - Success - Minimum params
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\nrunCode({\n event: 'tx', \n host:'https://twenga.twgdns.com',\
\ \n masterSiteId:undefined,\n currency:undefined,\n order_id:123,\n order_items:undefined,\n\
\ order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
- name: Transaction - Success - Missing variant_id
code: "mock('sendPixel', function(url, onSuccess, onFailure) {\n onSuccess();\n\
});\n\nmock('queryPermission', true);\n\nrunCode({\n event: 'tx', \n host:'https://twenga.twgdns.com',\
\ \n masterSiteId:666,\n currency:'PLN',\n order_id:123,\n order_items:[{'price':10.00,'id':666666,'quantity':1,'variant_id':undefined},{'price':15.99,'id':784469,'quantity':8,'variant_id':undefined},{'price':15048.43,'id':13548,'quantity':2,'variant_id':undefined}],\n\
\ order_attribution_weight:1,\n item_ref_id_key:'id',\n item_price_key:'price',\n\
\ item_quantity_key:'quantity',\n item_variant_id:0,\n product_price:0.25,\n\
\ product_ref_id:123,\n product_variant_id:0\n});\n\n// Verify that the tag\
\ finished successfully.\nassertApi('gtmOnSuccess').wasCalled();"
___NOTES___
Created on 26/10/2020 à 10:11:37