@penguin-wolf/braze-integration 2.1.0 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/braze-integration.js +88 -12
- package/package.json +1 -1
package/braze-integration.js
CHANGED
|
@@ -6,7 +6,7 @@ if(!window.BrazeIntegration) {
|
|
|
6
6
|
class BrazeIntegration {
|
|
7
7
|
static instance;
|
|
8
8
|
static version = packageJson.version;
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
constructor(sdkKey, sdkUrl, enableLogging=false) {
|
|
11
11
|
if(!sdkKey || !sdkUrl) {
|
|
12
12
|
console.error('SDK Key and SDK URL are required to construct BrazeIntegration');
|
|
@@ -17,27 +17,27 @@ if(!window.BrazeIntegration) {
|
|
|
17
17
|
// check if sdkKey and sdkUrl match the existing instance
|
|
18
18
|
if(BrazeIntegration.instance.sdkKey != sdkKey && BrazeIntegration.instance.sdkUrl != sdkUrl) {
|
|
19
19
|
throw new Error('BrazeIntegration already initialized with different SDK Key or SDK URL');
|
|
20
|
-
}
|
|
20
|
+
}
|
|
21
21
|
return BrazeIntegration.instance;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
|
|
24
24
|
this.sdkKey = sdkKey;
|
|
25
25
|
this.sdkUrl = sdkUrl;
|
|
26
|
-
this.enableLogging = enableLogging;
|
|
26
|
+
this.enableLogging = enableLogging;
|
|
27
27
|
this.braze = braze;
|
|
28
28
|
|
|
29
29
|
if(this.enableLogging) {
|
|
30
30
|
window.braze = braze;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
|
|
33
33
|
BrazeIntegration.instance = this;
|
|
34
34
|
|
|
35
35
|
this.initialize();
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
initialize() {
|
|
39
39
|
if(this._initialized) return;
|
|
40
|
-
|
|
40
|
+
|
|
41
41
|
braze.initialize(this.sdkKey, {
|
|
42
42
|
baseUrl: this.sdkUrl,
|
|
43
43
|
enableLogging: this.enableLogging,
|
|
@@ -46,12 +46,41 @@ if(!window.BrazeIntegration) {
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
this.changeUserFromUrlParameter();
|
|
49
|
-
|
|
49
|
+
|
|
50
50
|
braze.openSession();
|
|
51
|
-
|
|
51
|
+
|
|
52
|
+
this.applyBannerImpressionFix();
|
|
53
|
+
this.setupBannerImpressionLogging();
|
|
54
|
+
|
|
52
55
|
this._initialized = true;
|
|
53
56
|
}
|
|
54
57
|
|
|
58
|
+
/*
|
|
59
|
+
* Fix for Braze SDK bug: freshly-inserted banner iframes return a {0,0,0,0}
|
|
60
|
+
* bounding rect before the browser has computed layout, which trivially
|
|
61
|
+
* satisfies the SDK's viewport check and causes an impression to be logged
|
|
62
|
+
* for every banner on first insert — regardless of actual visibility.
|
|
63
|
+
*
|
|
64
|
+
* We patch Element.prototype.getBoundingClientRect to detect this pathological
|
|
65
|
+
* case for banner elements specifically and substitute an off-screen rect.
|
|
66
|
+
* The SDK's later scroll-triggered checks see the real rects and log accurately.
|
|
67
|
+
*
|
|
68
|
+
* Always runs (not gated on enableLogging) — this is a bug workaround, not debug.
|
|
69
|
+
*/
|
|
70
|
+
applyBannerImpressionFix() {
|
|
71
|
+
const original = Element.prototype.getBoundingClientRect;
|
|
72
|
+
Element.prototype.getBoundingClientRect = function() {
|
|
73
|
+
const rect = original.call(this);
|
|
74
|
+
if(
|
|
75
|
+
(this.classList?.contains('ab-html-banner') || this.classList?.contains('ab-html-control-banner'))
|
|
76
|
+
&& rect.top === 0 && rect.bottom === 0 && rect.width === 0 && rect.height === 0
|
|
77
|
+
) {
|
|
78
|
+
return new DOMRect(0, -99999, 0, 0);
|
|
79
|
+
}
|
|
80
|
+
return rect;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
55
84
|
changeUserFromUrlParameter() {
|
|
56
85
|
const urlParams = new URLSearchParams(window.location.search);
|
|
57
86
|
if(urlParams.has('external_id')) {
|
|
@@ -69,6 +98,53 @@ if(!window.BrazeIntegration) {
|
|
|
69
98
|
return false;
|
|
70
99
|
}
|
|
71
100
|
|
|
101
|
+
/*
|
|
102
|
+
* Banner impression logging
|
|
103
|
+
*
|
|
104
|
+
* The Braze SDK is silent on successful banner impressions — it only logs when
|
|
105
|
+
* dedup suppresses one ("Not logging banners impression for ID X..."). This
|
|
106
|
+
* watches the SDK's own impression-marker attributes (set inside
|
|
107
|
+
* detectBannerImpressions) and emits a log the moment a banner is actually
|
|
108
|
+
* marked as impressed, with a clear FULLY IMPRESSED indicator when both top
|
|
109
|
+
* and bottom markers have been set (which is the moment logBannerImpressions
|
|
110
|
+
* is called internally).
|
|
111
|
+
*
|
|
112
|
+
* Gated on enableLogging so it only runs in dev/debug environments.
|
|
113
|
+
*/
|
|
114
|
+
setupBannerImpressionLogging() {
|
|
115
|
+
if(!this.enableLogging) return;
|
|
116
|
+
|
|
117
|
+
const TOP_ATTR = 'data-ab-had-top-impression';
|
|
118
|
+
const BOTTOM_ATTR = 'data-ab-had-bottom-impression';
|
|
119
|
+
const PLACEMENT_ID_ATTR = 'data-ab-banner-placement-id';
|
|
120
|
+
|
|
121
|
+
const observer = new MutationObserver(mutations => {
|
|
122
|
+
for(const m of mutations) {
|
|
123
|
+
if(m.type !== 'attributes') continue;
|
|
124
|
+
const attr = m.attributeName;
|
|
125
|
+
if(attr !== TOP_ATTR && attr !== BOTTOM_ATTR) continue;
|
|
126
|
+
|
|
127
|
+
const el = m.target;
|
|
128
|
+
const placementId = el.getAttribute(PLACEMENT_ID_ATTR) || '(none)';
|
|
129
|
+
const edge = attr === TOP_ATTR ? 'TOP' : 'BOTTOM';
|
|
130
|
+
const topNow = el.getAttribute(TOP_ATTR) === 'true';
|
|
131
|
+
const bottomNow = el.getAttribute(BOTTOM_ATTR) === 'true';
|
|
132
|
+
const fullyImpressed = topNow && bottomNow;
|
|
133
|
+
|
|
134
|
+
console.log(
|
|
135
|
+
`%cBraze: banner impression marker — ${placementId} ${edge}${fullyImpressed ? ' ⇒ FULLY IMPRESSED' : ''}`,
|
|
136
|
+
`color:${fullyImpressed ? '#2ECC71' : '#F39C12'};font-weight:bold`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
observer.observe(document.documentElement, {
|
|
142
|
+
attributes: true,
|
|
143
|
+
attributeFilter: [TOP_ATTR, BOTTOM_ATTR],
|
|
144
|
+
subtree: true,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
72
148
|
log() {
|
|
73
149
|
if(!this.enableLogging) return;
|
|
74
150
|
console.log(...arguments);
|
|
@@ -77,11 +153,11 @@ if(!window.BrazeIntegration) {
|
|
|
77
153
|
}
|
|
78
154
|
|
|
79
155
|
window.BrazeIntegration = BrazeIntegration;
|
|
80
|
-
}
|
|
156
|
+
}
|
|
81
157
|
else {
|
|
82
158
|
if(window.BrazeIntegration.version !== packageJson.version) {
|
|
83
159
|
console.warn('Warning: BrazeIntegration version mismatch. BrazeIntegration has been loaded in multiple different versions which may cause issues, ensure every plugin is using the same version to avoid issues');
|
|
84
160
|
}
|
|
85
|
-
}
|
|
161
|
+
}
|
|
86
162
|
|
|
87
|
-
export default window.BrazeIntegration;
|
|
163
|
+
export default window.BrazeIntegration;
|