-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
180 lines (163 loc) · 7.34 KB
/
template.js
File metadata and controls
180 lines (163 loc) · 7.34 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const callInWindow = require('callInWindow');
const setInWindow = require('setInWindow');
const copyFromWindow = require('copyFromWindow');
const logToConsole = require('logToConsole');
const getType = require('getType');
const JSON = require('JSON');
const Object = require('Object');
const containerVersion = require('getContainerVersion')();
/**
* Logs messages to the console based on debug mode or user preference.
* Only logs to console, if:
* - is on preview and debug mode, always
* or
* - production, if the checkbox data.enableLog is checked.
* @param {string} message - The log message to display.
* @param {*} content - Additional content to log, such as objects or variables.
*/
const log = (message, content) => {
const logIdentifier = '[GA4 Unified Tag for Webview (Web & App) | Firebase Analytics Handler Global Variable Initialization] |';
const isInPreviewOrDebugMode = containerVersion.previewMode || containerVersion.debugMode;
if (data.enableLog || isInPreviewOrDebugMode) logToConsole(logIdentifier, message, content);
};
/**
* Initializes the Firebase Analytics Handler global variable for native communication.
*/
const initializeFirebaseAnalyticsHandlerGlobal = () => {
log('initializeFirebaseAnalyticsHandlerGlobal', 'Initializing JS handler global object.');
// Check if the Firebase Analytics Handler global variable already exists
const firebaseAnalyticsHandlerName = data.firebaseAnalyticsHandlerName || 'firebaseAnalyticsHandler';
let firebaseAnalyticsHandler = copyFromWindow(firebaseAnalyticsHandlerName);
if (!firebaseAnalyticsHandler) {
// Define the Firebase Analytics Handler global variable
firebaseAnalyticsHandler = {
/**
* Calls a native method for Firebase Analytics.
*
* @param {string} command - The command to execute (e.g., 'logEvent').
* @param {Object} data - The data to pass to the native method.
*/
_callNativeMethod: function (command, params) {
// Check for the interfaces on every run to overcome problems if they were defined late.
const androidInterfaceName = data.firebaseAnalyticsInterfaceNameAndroid || 'AnalyticsWebInterface';
const iOSInterfaceName = data.firebaseAnalyticsInterfaceNameIOS || 'firebase';
const androidInterface = copyFromWindow(androidInterfaceName);
const iOSInterface = copyFromWindow('webkit.messageHandlers.' + iOSInterfaceName);
const commonInterfaceName = data.firebaseAnalyticsInterfaceNameCommon;
const commonInterface = commonInterfaceName && copyFromWindow(commonInterfaceName);
log('_callNativeMethod | interface: ', {
iOSInterface: iOSInterface,
androidInterface: androidInterface,
commonInterface: commonInterface
});
log('_callNativeMethod | command and params: ', { command: command, params: params });
if (androidInterface) {
// Android native interface
switch(command) {
case 'logEvent':
callInWindow(androidInterfaceName + '.logEvent', params.name, JSON.stringify(params.parameters));
break;
case 'setUserProperty':
callInWindow(androidInterfaceName + '.setUserProperty', params.name, params.value);
break;
case 'setDefaultEventParameters':
callInWindow(androidInterfaceName + '.setDefaultEventParameters', JSON.stringify(params.parameters));
break;
case 'setUserId':
callInWindow(androidInterfaceName + '.setUserId', params.userId);
break;
case 'setAnalyticsCollectionEnabled':
callInWindow(androidInterfaceName + '.setAnalyticsCollectionEnabled', params.value);
break;
case 'resetAnalyticsData':
callInWindow(androidInterfaceName + '.resetAnalyticsData');
break;
case 'setConsent':
callInWindow(androidInterfaceName + '.setConsent', JSON.stringify(params.consentSettings));
break;
}
} else if (iOSInterface) {
// iOS native interface
const message = { command: command };
Object.keys(params || {}).forEach((key) => message[key] = params[key]);
callInWindow('webkit.messageHandlers.' + iOSInterfaceName + '.postMessage', message);
} else if (commonInterface) {
// Common (iOS and Android) native interface
const message = { command: command };
Object.keys(params || {}).forEach((key) => message[key] = params[key]);
callInWindow(commonInterfaceName, JSON.stringify(message));
} else {
log('_callNativeMethod', 'No native APIs found.');
}
},
/**
* Logs an event to Firebase Analytics.
*
* @param {string} name - The name of the event.
* @param {Object} params - The event parameters.
*/
logEvent: function (name, params) {
if (!name) return;
firebaseAnalyticsHandler._callNativeMethod('logEvent', { name: name, parameters: params });
},
/**
* Sets a user property in Firebase Analytics.
*
* @param {string} name - The name of the user property.
* @param {*} value - The value of the user property.
*/
setUserProperty: function (name, value) {
if (!name || value === undefined) return;
firebaseAnalyticsHandler._callNativeMethod('setUserProperty', { name: name, value: value });
},
/**
* Sets default event parameters for Firebase Analytics.
*
* @param {Object} params - The default parameters.
*/
setDefaultEventParameters: function (params) {
if (!params) return;
firebaseAnalyticsHandler._callNativeMethod('setDefaultEventParameters', { parameters: params });
},
/**
* Sets the user ID in Firebase Analytics.
*
* @param {string} userId - The user ID.
*/
setUserId: function (userId) {
if (userId === undefined) return;
firebaseAnalyticsHandler._callNativeMethod('setUserId', { userId: userId });
},
/**
* Enables or disables Firebase Analytics data collection.
*
* @param {boolean} value - Whether to enable or disable data collection.
*/
setAnalyticsCollectionEnabled: function (value) {
if (getType(value) !== 'boolean') return;
firebaseAnalyticsHandler._callNativeMethod('setAnalyticsCollectionEnabled', { value: value });
},
/**
* Resets Firebase Analytics data.
*/
resetAnalyticsData: function () {
firebaseAnalyticsHandler._callNativeMethod('resetAnalyticsData');
},
/**
* Sets consent settings for Firebase Analytics.
*
* @param {Object} consentSettings - The consent settings.
*/
setConsent: function (consentSettings) {
if (!consentSettings) return;
firebaseAnalyticsHandler._callNativeMethod('setConsent', { consentSettings: consentSettings });
},
};
log('JS Handler global object not detected. Initialized global object:', firebaseAnalyticsHandlerName);
// Set the Firebase Analytics Handler in the global scope
setInWindow(firebaseAnalyticsHandlerName, firebaseAnalyticsHandler, true);
}
};
log('data:', data);
initializeFirebaseAnalyticsHandlerGlobal();
data.gtmOnSuccess();