@penguin-wolf/braze-integration 1.0.1
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/README.md +19 -0
- package/braze-integration.js +81 -0
- package/package.json +15 -0
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Basic Usage
|
|
2
|
+
1. install using `npm i @penguinwolf/braze-integration`
|
|
3
|
+
2. import into your project and construct the integration
|
|
4
|
+
```js
|
|
5
|
+
import BrazeIntegration from "@penguinwolf/braze-integration";
|
|
6
|
+
|
|
7
|
+
const sdkKey = '...';
|
|
8
|
+
const sdkUrl = '...';
|
|
9
|
+
const enableLogging = scriptData.WP_ENV === 'development'; // note: only enable logging on development environments
|
|
10
|
+
|
|
11
|
+
const brazeIntegration = new BrazeIntegration(sdkKey, sdkUrl, enableLogging);
|
|
12
|
+
```
|
|
13
|
+
3. You can access the Braze SDK instance directly from the brazeIntegration object
|
|
14
|
+
```js
|
|
15
|
+
const braze = brazeIntegration.braze;
|
|
16
|
+
braze.logCustomEvent('set_location', {
|
|
17
|
+
location: 'QLD',
|
|
18
|
+
});
|
|
19
|
+
```
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as braze from "@braze/web-sdk";
|
|
2
|
+
import packageJson from "./package.json";
|
|
3
|
+
|
|
4
|
+
if(!window.BrazeIntegration) {
|
|
5
|
+
|
|
6
|
+
class BrazeIntegration {
|
|
7
|
+
static instance;
|
|
8
|
+
static version = packageJson.version;
|
|
9
|
+
|
|
10
|
+
constructor(sdkKey, sdkUrl, enableLogging=false) {
|
|
11
|
+
if(!sdkKey || !sdkUrl) {
|
|
12
|
+
console.error('SDK Key and SDK URL are required to construct BrazeIntegration');
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if(BrazeIntegration.instance) {
|
|
17
|
+
// check if sdkKey and sdkUrl match the existing instance
|
|
18
|
+
if(BrazeIntegration.instance.sdkKey != sdkKey && BrazeIntegration.instance.sdkUrl != sdkUrl) {
|
|
19
|
+
throw new Error('BrazeIntegration already initialized with different SDK Key or SDK URL');
|
|
20
|
+
}
|
|
21
|
+
return BrazeIntegration.instance;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.sdkKey = sdkKey;
|
|
25
|
+
this.sdkUrl = sdkUrl;
|
|
26
|
+
this.enableLogging = enableLogging;
|
|
27
|
+
this.braze = braze;
|
|
28
|
+
|
|
29
|
+
if(this.enableLogging) {
|
|
30
|
+
window.braze = braze;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
BrazeIntegration.instance = this;
|
|
34
|
+
|
|
35
|
+
this.initialize();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
initialize() {
|
|
39
|
+
if(this._initialized) return;
|
|
40
|
+
|
|
41
|
+
braze.initialize(this.sdkKey, {
|
|
42
|
+
baseUrl: this.sdkUrl,
|
|
43
|
+
enableLogging: this.enableLogging,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
this.changeUserFromUrlParameter();
|
|
47
|
+
|
|
48
|
+
braze.openSession();
|
|
49
|
+
|
|
50
|
+
this._initialized = true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
changeUserFromUrlParameter() {
|
|
54
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
55
|
+
if(urlParams.has('external_id')) {
|
|
56
|
+
const external_id = urlParams.get('external_id');
|
|
57
|
+
|
|
58
|
+
// Call braze changeUser
|
|
59
|
+
braze.changeUser(external_id);
|
|
60
|
+
|
|
61
|
+
// Remove parameter from URL
|
|
62
|
+
urlParams.delete('external_id');
|
|
63
|
+
window.history.replaceState({}, document.title, window.location.pathname + urlParams.toString());
|
|
64
|
+
|
|
65
|
+
return external_id;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
window.BrazeIntegration = BrazeIntegration;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
if(window.BrazeIntegration.version !== packageJson.version) {
|
|
76
|
+
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');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
export default window.BrazeIntegration;
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@penguin-wolf/braze-integration",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"main": "braze-integration.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"author": "",
|
|
9
|
+
"license": "ISC",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@braze/web-sdk": "^4.9.0"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {},
|
|
14
|
+
"description": ""
|
|
15
|
+
}
|