-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Embed Thank You Element in Joint SDK #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,7 +137,8 @@ interface MParticleExtended { | |
|
|
||
| interface TestHelpers { | ||
| generateLauncherScript: (domain: string | undefined, extensions: string[]) => string; | ||
| extractRoktExtensions: (settingsString?: string) => string[]; | ||
| generateThankYouElementScript: (domain: string | undefined) => string; | ||
| extractRoktExtensionConfig: (settingsString?: string) => RoktExtensionConfig; | ||
| hashEventMessage: (messageType: number, eventType: number, eventName: string) => string | number; | ||
| parseSettingsString: <T>(settingsString?: string) => T[]; | ||
| generateMappedEventLookup: (placementEventMapping: PlacementEventMappingEntry[]) => Record<string, string>; | ||
|
|
@@ -178,6 +179,12 @@ interface LogEntry { | |
| code?: string; | ||
| } | ||
|
|
||
| interface RoktExtensionConfig { | ||
| roktExtensionsQueryParams: string[]; | ||
| legacyRoktExtensions: string[]; | ||
| loadThankYouElement: boolean; | ||
| } | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| Rokt?: RoktGlobal; | ||
|
|
@@ -206,6 +213,9 @@ const ROKT_IDENTITY_EVENT_TYPE = { | |
| MODIFY_USER: 'modify_user', | ||
| IDENTIFY: 'identify', | ||
| } as const; | ||
| const ROKT_THANK_YOU_JOURNEY_EXTENSION = 'ThankYouJourney'; | ||
| const ROKT_INTEGRATION_SCRIPT_ID = 'rokt-launcher'; | ||
| const ROKT_THANK_YOU_ELEMENT_SCRIPT_ID = 'rokt-thank-you-element'; | ||
|
|
||
| type RoktIdentityEventType = (typeof ROKT_IDENTITY_EVENT_TYPE)[keyof typeof ROKT_IDENTITY_EVENT_TYPE]; | ||
|
|
||
|
|
@@ -245,17 +255,48 @@ function mp(): MParticleExtended { | |
| // ============================================================ | ||
|
|
||
| function generateLauncherScript(domain: string | undefined, extensions: string[]): string { | ||
| const resolvedDomain = typeof domain !== 'undefined' ? domain : 'apps.rokt.com'; | ||
| const protocol = 'https://'; | ||
| const launcherPath = '/wsdk/integrations/launcher.js'; | ||
| const baseUrl = [protocol, resolvedDomain, launcherPath].join(''); | ||
| const baseUrl = [generateBaseUrl(domain), launcherPath].join(''); | ||
|
|
||
| if (!extensions || extensions.length === 0) { | ||
| return baseUrl; | ||
| } | ||
| return baseUrl + '?extensions=' + extensions.join(','); | ||
| } | ||
|
|
||
| function generateThankYouElementScript(domain: string | undefined) { | ||
| const thankYouElementPath = '/rokt-elements/rokt-element-thank-you.js'; | ||
| return [generateBaseUrl(domain), thankYouElementPath].join(''); | ||
| } | ||
|
|
||
| function generateBaseUrl(domain: string | undefined) { | ||
| const resolvedDomain = typeof domain !== 'undefined' ? domain : 'apps.rokt.com'; | ||
| const protocol = 'https://'; | ||
|
|
||
| return [protocol, resolvedDomain].join(''); | ||
| } | ||
|
|
||
| function loadRoktScript(scriptId: string, source: string, appendToTarget: boolean = true) { | ||
| const preexistingScript = document.getElementById(scriptId); | ||
| if (preexistingScript) { | ||
| return preexistingScript; | ||
| } | ||
|
|
||
| const target = document.head || document.body; | ||
| const script = document.createElement('script'); | ||
| script.type = 'text/javascript'; | ||
| (script as HTMLScriptElement & { fetchPriority: string }).fetchPriority = 'high'; | ||
| script.src = source; | ||
| script.crossOrigin = 'anonymous'; | ||
| script.async = true; | ||
| script.id = scriptId; | ||
| if (appendToTarget) { | ||
| target.appendChild(script); | ||
| } | ||
|
|
||
| return script; | ||
| } | ||
|
|
||
| function isObject(val: unknown): val is Record<string, unknown> { | ||
| return val != null && typeof val === 'object' && Array.isArray(val) === false; | ||
| } | ||
|
|
@@ -272,15 +313,33 @@ function parseSettingsString<T>(settingsString?: string): T[] { | |
| return []; | ||
| } | ||
|
|
||
| function extractRoktExtensions(settingsString?: string): string[] { | ||
| function extractRoktExtensionConfig(settingsString?: string): RoktExtensionConfig { | ||
| const settings = settingsString ? parseSettingsString<RoktExtensionEntry>(settingsString) : []; | ||
| const roktExtensions: string[] = []; | ||
| const roktExtensionsQueryParams: string[] = []; | ||
| const legacyRoktExtensions: string[] = []; | ||
| let loadThankYouElement = false; | ||
|
|
||
| for (let i = 0; i < settings.length; i++) { | ||
| roktExtensions.push(settings[i].value); | ||
| const extensionName = settings[i].value; | ||
| if (extensionName === 'thank-you-journey') { | ||
| loadThankYouElement = true; | ||
| legacyRoktExtensions.push(ROKT_THANK_YOU_JOURNEY_EXTENSION) | ||
| } else { | ||
| roktExtensionsQueryParams.push(extensionName); | ||
| } | ||
| } | ||
|
|
||
| return roktExtensions; | ||
| return { | ||
| roktExtensionsQueryParams, | ||
| legacyRoktExtensions, | ||
| loadThankYouElement, | ||
| }; | ||
| } | ||
|
|
||
| function registerLegacyExtensions(legacyExtensions: string[]) { | ||
| for (const extension of legacyExtensions) { | ||
| window.mParticle.Rokt.use(extension); | ||
| } | ||
| } | ||
|
Comment on lines
+339
to
+343
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? I thought we are doing this in the new script creation.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. This is necessary to initialize the WSDK thank you page extension manager. |
||
|
|
||
| function generateMappedEventLookup(placementEventMapping: PlacementEventMappingEntry[]): Record<string, string> { | ||
|
|
@@ -954,7 +1013,6 @@ class RoktKit implements KitInterface { | |
| ): string { | ||
| const kitSettings = settings as unknown as RoktKitSettings; | ||
| const accountId = kitSettings.accountId; | ||
| const roktExtensions = extractRoktExtensions(kitSettings.roktExtensions); | ||
| this.userAttributes = filteredUserAttributes || {}; | ||
| this._onboardingExpProvider = kitSettings.onboardingExpProvider; | ||
|
|
||
|
|
@@ -972,6 +1030,11 @@ class RoktKit implements KitInterface { | |
| } | ||
|
|
||
| const domain = mp().Rokt?.domain; | ||
| const { | ||
| roktExtensionsQueryParams, | ||
| legacyRoktExtensions, | ||
| loadThankYouElement, | ||
| } = extractRoktExtensionConfig(kitSettings.roktExtensions); | ||
| const launcherOptions: Record<string, unknown> = { | ||
| ...((mp().Rokt?.launcherOptions as Record<string, unknown>) || {}), | ||
| }; | ||
|
|
@@ -1012,7 +1075,8 @@ class RoktKit implements KitInterface { | |
| if (testMode) { | ||
| this.testHelpers = { | ||
| generateLauncherScript: generateLauncherScript, | ||
| extractRoktExtensions: extractRoktExtensions, | ||
| generateThankYouElementScript: generateThankYouElementScript, | ||
| extractRoktExtensionConfig: extractRoktExtensionConfig, | ||
| hashEventMessage: hashEventMessage, | ||
| parseSettingsString: parseSettingsString, | ||
| generateMappedEventLookup: generateMappedEventLookup, | ||
|
|
@@ -1034,21 +1098,25 @@ class RoktKit implements KitInterface { | |
| return 'Successfully initialized: ' + name; | ||
| } | ||
|
|
||
| if (loadThankYouElement) { | ||
| loadRoktScript( | ||
| ROKT_THANK_YOU_ELEMENT_SCRIPT_ID, generateThankYouElementScript(domain)); | ||
| } | ||
|
|
||
| if (this.isLauncherReadyToAttach()) { | ||
| this.attachLauncher(accountId, launcherOptions); | ||
| } else { | ||
| const target = document.head || document.body; | ||
| const script = document.createElement('script'); | ||
| script.type = 'text/javascript'; | ||
| script.src = generateLauncherScript(domain, roktExtensions); | ||
| script.async = true; | ||
| script.crossOrigin = 'anonymous'; | ||
| (script as HTMLScriptElement & { fetchPriority: string }).fetchPriority = 'high'; | ||
| script.id = 'rokt-launcher'; | ||
| const script = loadRoktScript( | ||
| ROKT_INTEGRATION_SCRIPT_ID, | ||
| generateLauncherScript(domain, roktExtensionsQueryParams), | ||
| false, | ||
| ) | ||
|
|
||
| script.onload = () => { | ||
| if (this.isLauncherReadyToAttach()) { | ||
| this.attachLauncher(accountId, launcherOptions); | ||
| registerLegacyExtensions(legacyRoktExtensions); | ||
| } else { | ||
| console.error('Rokt object is not available after script load.'); | ||
| } | ||
|
|
@@ -1200,6 +1268,8 @@ class RoktKit implements KitInterface { | |
|
|
||
| /** | ||
| * Enables optional Integration Launcher extensions before selecting placements. | ||
| * | ||
| * @deprecated This functionality has been internalized and will be removed in a future release. | ||
| */ | ||
| public use(extensionName: string): Promise<unknown> { | ||
| if (!this.isKitReady()) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this ever be undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it will be if partners don't define
ROKT_DOMAIN.