-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrazeJavascriptBridge.js
More file actions
124 lines (107 loc) · 3.78 KB
/
brazeJavascriptBridge.js
File metadata and controls
124 lines (107 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Braze JavaScript Bridge for Flutter WebView
*
* This bridge provides the same interface as the Braze Web SDK,
* but forwards all calls to the native Braze SDK through Flutter's WebView handler.
*/
class BrazeJavascriptBridge {
constructor() {
this.handlerName = 'brazeHandler';
this.initialized = false;
// Initialize the bridge
this.init();
}
/**
* Initialize the bridge
*/
init() {
if (this.initialized) {
return;
}
// Check if we're in a Flutter WebView environment
if (typeof window !== 'undefined' && window.flutter_inappwebview) {
this.initialized = true;
console.log('Braze JavaScript Bridge initialized');
} else {
console.warn('Braze JavaScript Bridge: Flutter WebView not detected');
}
}
/**
* Send message to Flutter native layer
*/
sendToNative(method, data) {
if (!this.initialized) {
console.error('Braze JavaScript Bridge: Not initialized');
return;
}
try {
const message = {
method: method,
data: data,
timestamp: Date.now()
};
// Send to Flutter WebView handler
if (window.flutter_inappwebview && window.flutter_inappwebview.callHandler) {
window.flutter_inappwebview.callHandler(this.handlerName, message);
} else {
console.error('Braze JavaScript Bridge: Flutter handler not available');
}
} catch (error) {
console.error('Braze JavaScript Bridge: Error sending message', error);
}
}
/**
* Log a custom event
* @param {string} eventName - Name of the event
* @param {Object} properties - Event properties (optional)
*/
logCustomEvent(eventName, properties = {}) {
if (!eventName || typeof eventName !== 'string') {
console.error('Braze JavaScript Bridge: Event name is required and must be a string');
return;
}
this.sendToNative('logCustomEvent', {
eventName: eventName,
properties: properties || {}
});
}
/**
* Log a purchase
* @param {string} productId - Product identifier
* @param {number} price - Price of the product
* @param {string} currency - Currency code (e.g., 'USD')
* @param {number} quantity - Quantity purchased (optional, defaults to 1)
* @param {Object} properties - Purchase properties (optional)
*/
logPurchase(productId, price, currency, quantity = 1, properties = {}) {
if (!productId || typeof productId !== 'string') {
console.error('Braze JavaScript Bridge: Product ID is required and must be a string');
return;
}
if (typeof price !== 'number' || price < 0) {
console.error('Braze JavaScript Bridge: Price must be a positive number');
return;
}
if (!currency || typeof currency !== 'string') {
console.error('Braze JavaScript Bridge: Currency is required and must be a string');
return;
}
if (typeof quantity !== 'number' || quantity < 1) {
console.error('Braze JavaScript Bridge: Quantity must be a positive number');
return;
}
this.sendToNative('logPurchase', {
productId: productId,
price: price,
currency: currency.toUpperCase(),
quantity: quantity,
properties: properties || {}
});
}
}
// Create global braze instance
window.braze = new BrazeJavascriptBridge();
// Export for module systems
if (typeof module !== 'undefined' && module.exports) {
module.exports = BrazeJavascriptBridge;
}