Skip to content

Commit bdcd4c5

Browse files
authored
feat: add use method for rokt kit (#1067)
1 parent c46fcee commit bdcd4c5

File tree

8 files changed

+185
-32
lines changed

8 files changed

+185
-32
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ window.mParticle = {
4646
};
4747

4848
//load the SDK
49-
(function(e){window.mParticle=window.mParticle||{};window.mParticle.EventType={Unknown:0,Navigation:1,Location:2,Search:3,Transaction:4,UserContent:5,UserPreference:6,Social:7,Other:8,Media:9};window.mParticle.eCommerce={Cart:{}};window.mParticle.Identity={};window.mParticle.Rokt={};window.mParticle.config=window.mParticle.config||{};window.mParticle.config.rq=[];window.mParticle.config.snippetVersion=2.5;window.mParticle.ready=function(e){window.mParticle.config.rq.push(e)};var t=["endSession","logError","logBaseEvent","logEvent","logForm","logLink","logPageView","setSessionAttribute","setAppName","setAppVersion","setOptOut","setPosition","startNewSession","startTrackingLocation","stopTrackingLocation"];var i=["setCurrencyCode","logCheckout"];var n=["identify","login","logout","modify"];var o=["selectPlacements","hashAttributes","setExtensionData"];t.forEach(function(e){window.mParticle[e]=r(e)});i.forEach(function(e){window.mParticle.eCommerce[e]=r(e,"eCommerce")});n.forEach(function(e){window.mParticle.Identity[e]=r(e,"Identity")});o.forEach(function(e){window.mParticle.Rokt[e]=r(e,"Rokt")});function r(t,i){return function(){if(i){t=i+"."+t}var e=Array.prototype.slice.call(arguments);e.unshift(t);window.mParticle.config.rq.push(e)}}var c,a,s=window.mParticle.config,l=s.isDevelopmentMode?1:0,w="?env="+l,d=window.mParticle.config.dataPlan;if(d){c=d.planId;a=d.planVersion;if(c){if(a&&(a<1||a>1e3)){a=null}w+="&plan_id="+c+(a?"&plan_version="+a:"")}}var m=window.mParticle.config.versions;var f=[];if(m){Object.keys(m).forEach(function(e){f.push(e+"="+m[e])})}var p=document.createElement("script");p.type="text/javascript";p.async=true;p.src=("https:"==document.location.protocol?"https://jssdkcdns":"http://jssdkcdn")+".mparticle.com/js/v2/"+e+"/mparticle.js"+w+"&"+f.join("&");var P=document.getElementsByTagName("script")[0];P.parentNode.insertBefore(p,P)})("REPLACE WITH API KEY");
50-
</script>
49+
(function(e){window.mParticle=window.mParticle||{};window.mParticle.EventType={Unknown:0,Navigation:1,Location:2,Search:3,Transaction:4,UserContent:5,UserPreference:6,Social:7,Other:8,Media:9};window.mParticle.eCommerce={Cart:{}};window.mParticle.Identity={};window.mParticle.Rokt={};window.mParticle.config=window.mParticle.config||{};window.mParticle.config.rq=[];window.mParticle.config.snippetVersion=2.7;window.mParticle.ready=function(e){window.mParticle.config.rq.push(e)};var t=["endSession","logError","logBaseEvent","logEvent","logForm","logLink","logPageView","setSessionAttribute","setAppName","setAppVersion","setOptOut","setPosition","startNewSession","startTrackingLocation","stopTrackingLocation"];var i=["setCurrencyCode","logCheckout"];var n=["identify","login","logout","modify"];var o=["selectPlacements","hashAttributes","setExtensionData","use","getVersion","terminate"];t.forEach(function(e){window.mParticle[e]=r(e)});i.forEach(function(e){window.mParticle.eCommerce[e]=r(e,"eCommerce")});n.forEach(function(e){window.mParticle.Identity[e]=r(e,"Identity")});o.forEach(function(e){window.mParticle.Rokt[e]=r(e,"Rokt")});function r(t,i){return function(){if(i){t=i+"."+t}var e=Array.prototype.slice.call(arguments);e.unshift(t);window.mParticle.config.rq.push(e)}}var a,c,s=window.mParticle.config,l=s.isDevelopmentMode?1:0,w="?env="+l,d=window.mParticle.config.dataPlan;if(d){a=d.planId;c=d.planVersion;if(a){if(c&&(c<1||c>1e3)){c=null}w+="&plan_id="+a+(c?"&plan_version="+c:"")}}var m=window.mParticle.config.versions;var f=[];if(m){Object.keys(m).forEach(function(e){f.push(e+"="+m[e])})}var p=document.createElement("script");p.type="text/javascript";p.async=true;p.src=("https:"==document.location.protocol?"https://jssdkcdns":"http://jssdkcdn")+".mparticle.com/js/v2/"+e+"/mparticle.js"+w+"&"+f.join("&");var P=document.getElementsByTagName("script")[0];P.parentNode.insertBefore(p,P)})("REPLACE WITH API KEY");
5150
```
5251

5352
You can then log events, for example, as follows:

snippet.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
window.mParticle.Rokt = {};
2020
window.mParticle.config = window.mParticle.config || {};
2121
window.mParticle.config.rq = [];
22-
window.mParticle.config.snippetVersion = 2.5;
22+
window.mParticle.config.snippetVersion = 2.7;
2323
window.mParticle.ready = function(f) {
2424
window.mParticle.config.rq.push(f);
2525
};
@@ -49,6 +49,9 @@
4949
'selectPlacements',
5050
'hashAttributes',
5151
'setExtensionData',
52+
'use',
53+
'getVersion',
54+
'terminate',
5255
];
5356

5457
// iterates through methods above to create stubs

snippet.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

snippet.rokt.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
window.mParticle.Rokt = {};
2020
window.mParticle.config = window.mParticle.config || {};
2121
window.mParticle.config.rq = [];
22-
window.mParticle.config.snippetVersion = 2.6;
22+
window.mParticle.config.snippetVersion = 2.7;
2323
window.mParticle.ready = function(f) {
2424
window.mParticle.config.rq.push(f);
2525
};
@@ -49,6 +49,9 @@
4949
'selectPlacements',
5050
'hashAttributes',
5151
'setExtensionData',
52+
'use',
53+
'getVersion',
54+
'terminate',
5255
];
5356

5457
// iterates through methods above to create stubs

snippet.rokt.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/roktManager.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface IRoktSelection {
3838
export interface IRoktLauncher {
3939
selectPlacements: (options: IRoktSelectPlacementsOptions) => Promise<IRoktSelection>;
4040
hashAttributes: (attributes: IRoktPartnerAttributes) => Promise<Record<string, string>>;
41+
use: <T>(name: string) => Promise<T>;
4142
}
4243

4344
export interface IRoktMessage {
@@ -62,6 +63,7 @@ export interface IRoktKit {
6263
hashAttributes: (attributes: IRoktPartnerAttributes) => Promise<Record<string, string>>;
6364
selectPlacements: (options: IRoktSelectPlacementsOptions) => Promise<IRoktSelection>;
6465
setExtensionData<T>(extensionData: IRoktPartnerExtensionData<T>): void;
66+
use: <T>(name: string) => Promise<T>;
6567
launcherOptions?: Dictionary<any>;
6668
}
6769

@@ -251,6 +253,18 @@ export default class RoktManager {
251253
}
252254
}
253255

256+
public use<T>(name: string): Promise<T> {
257+
if (!this.isReady()) {
258+
return this.deferredCall<T>('use', name);
259+
}
260+
261+
try {
262+
return this.kit.use<T>(name);
263+
} catch (error) {
264+
return Promise.reject(error instanceof Error ? error : new Error('Error using extension: ' + name));
265+
}
266+
}
267+
254268
public getLocalSessionAttributes(): LocalSessionAttributes {
255269
return this.store.getLocalSessionAttributes();
256270
}

0 commit comments

Comments
 (0)