Skip to content

Commit dc6cc85

Browse files
STRATCONN-6213 - [MoEngage] - Web destination initialize bug fix (#3679)
* fixing initialize * sd * refacting vendor code * sd * adding lint comments * attempting to fix loadig issue * lint issues * tested with Moengage team * updated type * fixing test
1 parent 7a44526 commit dc6cc85

4 files changed

Lines changed: 93 additions & 96 deletions

File tree

packages/browser-destinations/destinations/moengage-web/src/__tests__/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Moengage Web init', () => {
2020
// Mock window.moe to simulate SDK loading
2121
const mockMoe = jest.fn((_config) => {
2222
mockMoengage = {
23+
initialized: true,
2324
track_event: jest.fn(),
2425
add_user_attribute: jest.fn(),
2526
add_first_name: jest.fn(),
Lines changed: 84 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,96 @@
11
import { Settings } from './generated-types'
2+
import { MoengageSDK } from './types'
23

3-
export async function initializeSDK(settings: Settings) {
4-
const {
4+
export function initializeSDK(settings: Settings): Promise<void> {
5+
const {
56
moeDataCenter,
67
} = settings
78

8-
// MoEngage SDK loader snippet - vendor code
9-
/* eslint-disable */
10-
!(function (e, n, i, t, a, r, o, d) {
11-
if (!moeDataCenter || !moeDataCenter.match(/^dc_[0-9]+$/gm))
12-
return console.error( "Data center has not been passed correctly. Please follow the SDK installation instruction carefully." );
13-
// @ts-expect-error - vendor code
14-
var s = (e[a] = e[a] || []);
15-
if (((s.invoked = 0), s.initialised > 0 || s.invoked > 0))
16-
return (
17-
console.error(
18-
"MoEngage Web SDK initialised multiple times. Please integrate the Web SDK only once!"
19-
),
20-
!1
21-
);
22-
e.moengage_object = a;
23-
var l = {},
24-
// @ts-expect-error - vendor code
25-
g = function n(i) {
26-
return function () {
27-
for (var n = arguments.length, t = Array(n), a = 0; a < n; a++)
28-
t[a] = arguments[a];
29-
(e.moengage_q = e.moengage_q || []).push({ f: i, a: t });
9+
return new Promise<void>((resolve, reject) => {
10+
/* eslint-disable */
11+
const e = window
12+
const n = document
13+
const i = "script"
14+
const t = `https://cdn.moengage.com/release/${moeDataCenter}/moe_webSdk.min.latest.js`
15+
const a = "Moengage"
16+
let r: HTMLScriptElement
17+
let o: HTMLElement
18+
19+
if (!moeDataCenter || !moeDataCenter.match(/^dc_[0-9]+$/gm)){
20+
return console.error( "Data center has not been passed correctly. Please follow the SDK installation instruction carefully." )
21+
}
22+
// @ts-expect-error - vendor code: e[a] is a dynamic stub array before the real SDK loads
23+
var s = (e[a] = e[a] || [])
24+
// @ts-expect-error - vendor code: s.invoked and s.initialised are set by the real SDK at runtime
25+
if (((s.invoked = 0), s.initialised > 0 || s.invoked > 0)) {
26+
console.error("MoEngage Web SDK initialised multiple times. Please integrate the Web SDK only once!")
27+
return
28+
}
29+
e.moengage_object = a;
30+
var l = {};
31+
var g= function n(i: string) {
32+
return function () {
33+
for (var n = arguments.length, t = Array(n), a = 0; a < n; a++)
34+
t[a] = arguments[a];
35+
(e.moengage_q = e.moengage_q || []).push({ f: i, a: t });
36+
};
3037
};
31-
},
32-
u = [
33-
"track_event",
34-
"add_user_attribute",
35-
"add_first_name",
36-
"add_last_name",
37-
"add_email",
38-
"add_mobile",
39-
"add_user_name",
40-
"add_gender",
41-
"add_birthday",
42-
"destroy_session",
43-
"add_unique_user_id",
44-
"update_unique_user_id",
45-
"moe_events",
46-
"call_web_push",
47-
"track",
48-
"location_type_attribute",
49-
"identifyUser",
50-
"getUserIdentities",
51-
],
52-
m = { onsite: ["getData", "registerCallback", "getSelfHandledOSM"] };
53-
// @ts-expect-error - vendor code
54-
for (var c in u) l[u[c]] = g(u[c]);
55-
for (var v in m)
56-
// @ts-expect-error - vendor code
57-
for (var f in m[v])
38+
var u = [
39+
"track_event",
40+
"add_user_attribute",
41+
"add_first_name",
42+
"add_last_name",
43+
"add_email",
44+
"add_mobile",
45+
"add_user_name",
46+
"add_gender",
47+
"add_birthday",
48+
"destroy_session",
49+
"add_unique_user_id",
50+
"update_unique_user_id",
51+
"moe_events",
52+
"call_web_push",
53+
"track",
54+
"location_type_attribute",
55+
"identifyUser",
56+
"getUserIdentities"
57+
];
58+
var m = { onsite: ["getData", "registerCallback", "getSelfHandledOSM"] };
59+
5860
// @ts-expect-error - vendor code
59-
null == l[v] && (l[v] = {}), (l[v][m[v][f]] = g(v + "." + m[v][f]));
61+
for (var c in u) l[u[c]] = g(u[c]);
62+
63+
for (var v in m) {
6064
// @ts-expect-error - vendor code
61-
(r = n.createElement(i)),
62-
// @ts-expect-error - vendor code
63-
(o = n.getElementsByTagName("head")[0]),
64-
// @ts-expect-error - vendor code
65-
(r.async = 1),
66-
// @ts-expect-error - vendor code
67-
(r.src = t),
68-
// @ts-expect-error - vendor code
69-
o.appendChild(r),
70-
// @ts-expect-error - vendor code
71-
(e.moe =
72-
e.moe ||
73-
function () {
74-
return ((s.invoked = s.invoked + 1), s.invoked > 1)
75-
? (console.error(
76-
"MoEngage Web SDK initialised multiple times. Please integrate the Web SDK only once!"
77-
),
78-
!1)
79-
: ((d = arguments.length <= 0 ? void 0 : arguments[0]), l);
80-
}),
81-
// @ts-expect-error - vendor code
65+
for (var f in m[v]) {
66+
// @ts-expect-error - vendor code
67+
null == l[v] && (l[v] = {}), (l[v][m[v][f]] = g(v + "." + m[v][f]));
68+
}
69+
}
70+
71+
r = n.createElement(i)
72+
o = n.getElementsByTagName("head")[0]
73+
r.async = true
74+
r.src = t
75+
o.appendChild(r)
76+
e.moe = e.moe || function(): MoengageSDK {
77+
// @ts-expect-error - vendor code: s.invoked is set at runtime by the real SDK
78+
s.invoked = s.invoked + 1
79+
// @ts-expect-error - vendor code: s.invoked is set at runtime by the real SDK
80+
if (s.invoked > 1) {
81+
console.error("MoEngage Web SDK initialised multiple times. Please integrate the Web SDK only once!")
82+
}
83+
return l as unknown as MoengageSDK
84+
}
85+
8286
r.addEventListener("load", function () {
83-
if (d)
84-
return (
85-
// @ts-expect-error - vendor code
86-
(e[a] = e.moe(d)), (e[a].initialised = e[a].initialised + 1 || 1), !0
87-
);
88-
}),
89-
// @ts-expect-error - vendor code
87+
resolve()
88+
});
89+
9090
r.addEventListener("error", function () {
91-
return console.error("Moengage Web SDK loading failed."), !1;
91+
reject(new Error("Moengage Web SDK loading failed."))
9292
});
93-
})(
94-
window,
95-
document,
96-
"script",
97-
"https://cdn.moengage.com/release/" +
98-
moeDataCenter +
99-
"/moe_webSdk.min.latest.js",
100-
"Moengage"
101-
);
102-
/* eslint-enable */
93+
94+
/* eslint-enable */
95+
}) // end Promise
10396
}

packages/browser-destinations/destinations/moengage-web/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ export const destination: BrowserDestinationDefinition<Settings, MoengageSDK> =
171171
} = settings
172172

173173
await initializeSDK(settings)
174-
174+
175175
const initConfig: InitConfig = {
176-
app_id,
176+
app_id,
177177
env,
178178
...(project_id ? { project_id } : {}),
179179
...(typeof enableSPA === 'boolean' ? { enableSPA } : {}),
@@ -194,11 +194,13 @@ export const destination: BrowserDestinationDefinition<Settings, MoengageSDK> =
194194
}
195195

196196
await deps.resolveWhen(() => typeof window.moe === 'function', 100)
197-
197+
198198
if(window.moe) {
199199
window.Moengage = window.moe(initConfig)
200-
await deps.resolveWhen(() => typeof window?.Moengage?.onsite === 'function', 100)
201-
const client = window.Moengage
200+
201+
await deps.resolveWhen(() => window?.Moengage?.initialized === true, 100)
202+
203+
const client = window.Moengage
202204
const originalReset = analytics.reset
203205
analytics.reset = (...args) => {
204206
if (typeof originalReset === 'function') {

packages/browser-destinations/destinations/moengage-web/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface MoengageSDK {
1313
identifyUser(identifiers: string | Identifiers) : void
1414
getUserIdentities() : Record<string, unknown>
1515
onsite(): void
16+
initialized: boolean
1617
}
1718

1819
export interface Attributes {

0 commit comments

Comments
 (0)