Skip to content

Commit b41da8c

Browse files
committed
feat: update the GTM template
- allow use of development key (for different environments) - allow setting of user id before start - allow simple tracking of sessions (without tracking event / user attributes - allow setting of web push options
1 parent ae14ae9 commit b41da8c

File tree

1 file changed

+122
-12
lines changed

1 file changed

+122
-12
lines changed

build/gtm-template.tpl

Lines changed: 122 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,37 @@ ___TEMPLATE_PARAMETERS___
6565
{
6666
"type": "TEXT",
6767
"name": "productionKey",
68-
"displayName": "Production Key",
68+
"displayName": "Client secret",
6969
"simpleValueType": true,
7070
"valueValidators": [
71-
{
72-
"type": "REGEX",
73-
"args": [
74-
"^prod_.*"
75-
]
76-
},
7771
{
7872
"type": "NON_EMPTY"
7973
}
80-
]
74+
],
75+
"valueHint": "prod_abcdefgh...",
76+
"help": "Never use a non-production key on live websites."
8177
},
8278
{
8379
"type": "CHECKBOX",
8480
"name": "enableRichInAppMessages",
8581
"checkboxText": "Enable Rich In-App Messages",
8682
"simpleValueType": true,
8783
"help": "If enabled, rich in-app messages will be delivered to web users that match the trigger conditions of your campaigns."
84+
},
85+
{
86+
"type": "GROUP",
87+
"name": "webPush",
88+
"displayName": "Web Push options",
89+
"groupStyle": "NO_ZIPPY",
90+
"subParams": [
91+
{
92+
"type": "TEXT",
93+
"name": "webPushServiceWorkerUrl",
94+
"displayName": "Service Worker URL",
95+
"simpleValueType": true,
96+
"help": "Specify the location of the service worker file on the web server. This allows you to send messages that use the \u0027Register for Push\u0027 app function."
97+
}
98+
]
8899
}
89100
]
90101
},
@@ -104,6 +115,10 @@ ___TEMPLATE_PARAMETERS___
104115
"value": "load",
105116
"displayValue": "Only load SDK"
106117
},
118+
{
119+
"value": "loadAndStart",
120+
"displayValue": "Load and start session"
121+
},
107122
{
108123
"value": "track",
109124
"displayValue": "Track Event"
@@ -120,6 +135,20 @@ ___TEMPLATE_PARAMETERS___
120135
"simpleValueType": true,
121136
"subParams": []
122137
},
138+
{
139+
"type": "TEXT",
140+
"name": "userId",
141+
"displayName": "User ID",
142+
"simpleValueType": true,
143+
"help": "Leave blank to use previously-stored ID.\nFor new users, a new ID is generated at the start of their first session.",
144+
"enablingConditions": [
145+
{
146+
"paramName": "method",
147+
"paramValue": "load",
148+
"type": "NOT_EQUALS"
149+
}
150+
]
151+
},
123152
{
124153
"type": "GROUP",
125154
"name": "trackOptions",
@@ -291,8 +320,10 @@ const makeNumber = require('makeNumber');
291320
const LP_URL = 'https://cdn.jsdelivr.net/npm/leanplum-sdk@{LP_SDK_VERSION}/dist/leanplum.min.js';
292321

293322
// command queue
323+
var setKey = data.productionKey.indexOf("dev_") === 0 ?
324+
"setAppIdForDevelopmentMode" : "setAppIdForProductionMode";
294325
var queue = [
295-
{ "name": "setAppIdForProductionMode", "args": [data.applicationKey, data.productionKey] },
326+
{ "name": setKey, "args": [data.applicationKey, data.productionKey] },
296327
{ "name": "useSessionLength", "args": [2*60*60] }
297328
];
298329

@@ -302,10 +333,19 @@ if (data.enableRichInAppMessages) {
302333
);
303334
}
304335

336+
if (data.webPushServiceWorkerUrl) {
337+
queue.push(
338+
{ "name": "setWebPushOptions", "args": [{
339+
serviceWorkerUrl: data.webPushServiceWorkerUrl
340+
}] }
341+
);
342+
}
343+
305344
if (data.method !== "load") {
306345
// start a session
346+
var args = data.userId ? [data.userId] : [];
307347
queue.push(
308-
{ "name": "start", "args": [] }
348+
{ "name": "start", "args": args }
309349
);
310350
}
311351

@@ -497,7 +537,7 @@ ___WEB_PERMISSIONS___
497537
___TESTS___
498538

499539
scenarios:
500-
- name: Injects the Leanplum SDK script
540+
- name: Injects script
501541
code: |-
502542
runCode({
503543
applicationKey: "app_foo",
@@ -645,7 +685,6 @@ scenarios:
645685
{ "name": "setAppIdForProductionMode", "args": ["app_foo", "prod_bar"] },
646686
{ "name": "useSessionLength", "args": [2*60*60] }
647687
]);
648-
649688
- name: Can enable rich in-app message rendering
650689
code: |-
651690
mock('injectScript', function(url, onSuccess, onFailure) {
@@ -664,6 +703,77 @@ scenarios:
664703
{ "name": "useSessionLength", "args": [2*60*60] },
665704
{ "name": "enableRichInAppMessages", "args": [ true ] }
666705
]);
706+
- name: Can use development key
707+
code: |-
708+
mock('injectScript', function(url, onSuccess, onFailure) {
709+
onSuccess();
710+
});
711+
712+
runCode({
713+
applicationKey: "app_foo",
714+
productionKey: "dev_bar",
715+
method: "load"
716+
});
717+
718+
assertApi('callInWindow').wasCalledWith("Leanplum.applyQueue", [
719+
{ "name": "setAppIdForDevelopmentMode", "args": ["app_foo", "dev_bar"] },
720+
{ "name": "useSessionLength", "args": [2*60*60] }
721+
]);
722+
- name: Can track sessions
723+
code: |-
724+
mock('injectScript', function(url, onSuccess, onFailure) {
725+
onSuccess();
726+
});
727+
728+
runCode({
729+
applicationKey: "app_foo",
730+
productionKey: "prod_bar",
731+
method: "loadAndStart"
732+
});
733+
734+
assertApi('callInWindow').wasCalledWith("Leanplum.applyQueue", [
735+
{ "name": "setAppIdForProductionMode", "args": ["app_foo", "prod_bar"] },
736+
{ "name": "useSessionLength", "args": [2*60*60] },
737+
{ "name": "start", "args": [] }
738+
]);
739+
- name: Can use custom user ID
740+
code: |-
741+
mock('injectScript', function(url, onSuccess, onFailure) {
742+
onSuccess();
743+
});
744+
745+
runCode({
746+
applicationKey: "app_foo",
747+
productionKey: "prod_bar",
748+
method: "loadAndStart",
749+
userId: "from-data-layer"
750+
});
751+
752+
assertApi('callInWindow').wasCalledWith("Leanplum.applyQueue", [
753+
{ "name": "setAppIdForProductionMode", "args": ["app_foo", "prod_bar"] },
754+
{ "name": "useSessionLength", "args": [2*60*60] },
755+
{ "name": "start", "args": ["from-data-layer"] }
756+
]);
757+
- name: Can set Web Push options
758+
code: |-
759+
mock('injectScript', function(url, onSuccess, onFailure) {
760+
onSuccess();
761+
});
762+
763+
runCode({
764+
applicationKey: "app_foo",
765+
productionKey: "prod_bar",
766+
method: "loadAndStart",
767+
webPushServiceWorkerUrl: "/foo/sw.min.js",
768+
userId: "from-data-layer"
769+
});
770+
771+
assertApi('callInWindow').wasCalledWith("Leanplum.applyQueue", [
772+
{ "name": "setAppIdForProductionMode", "args": ["app_foo", "prod_bar"] },
773+
{ "name": "useSessionLength", "args": [2*60*60] },
774+
{ "name": "setWebPushOptions", "args": [{ serviceWorkerUrl: "/foo/sw.min.js" }] },
775+
{ "name": "start", "args": ["from-data-layer"] }
776+
]);
667777

668778

669779
___NOTES___

0 commit comments

Comments
 (0)