@armco/analytics 0.2.3 → 0.2.5
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/analytics.js +35 -26
- package/constants.d.ts +1 -1
- package/constants.js +1 -1
- package/index.interface.d.ts +2 -1
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -21,7 +21,8 @@ const packageJsonPath = getEnvironmentType() === "node" ? path.resolve(process.c
|
|
|
21
21
|
let ar_anonymous_id;
|
|
22
22
|
let user = null;
|
|
23
23
|
let apiKey = null;
|
|
24
|
-
let
|
|
24
|
+
let analyticsLogEndpoint;
|
|
25
|
+
let analyticsTagEndpoint;
|
|
25
26
|
let hostProjectName = null;
|
|
26
27
|
let enabled = null;
|
|
27
28
|
const CONFIG_FILE_NAME = "analyticsrc";
|
|
@@ -74,19 +75,19 @@ function loadConfiguration() {
|
|
|
74
75
|
return config;
|
|
75
76
|
}
|
|
76
77
|
catch (error) {
|
|
77
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
78
|
+
console.error(`[ANALYTICS] Failed to load configuration from ${configFilePath}.`);
|
|
78
79
|
}
|
|
79
80
|
const isTS = yield isTypeScriptProject();
|
|
80
81
|
configFilePath = `${ROOT}${CONFIG_FILE_NAME}.${isTS ? "ts" : "js"}`;
|
|
81
82
|
try {
|
|
82
83
|
const config = yield import(configFilePath);
|
|
83
|
-
console.log(`Configuration loaded from ${configFilePath} successfully.`);
|
|
84
|
+
console.log(`[ANALYTICS] Configuration loaded from ${configFilePath} successfully.`);
|
|
84
85
|
return config.default;
|
|
85
86
|
}
|
|
86
87
|
catch (error) {
|
|
87
|
-
console.error(`Failed to load configuration from ${configFilePath}.`);
|
|
88
|
+
console.error(`[ANALYTICS] Failed to load configuration from ${configFilePath}.`);
|
|
88
89
|
}
|
|
89
|
-
console.error(`No valid configuration file found. Expected one of ${CONFIG_FILE_NAME}.js, ${CONFIG_FILE_NAME}.json or ${CONFIG_FILE_NAME}.ts`);
|
|
90
|
+
console.error(`[ANALYTICS] No valid configuration file found. Expected one of ${CONFIG_FILE_NAME}.js, ${CONFIG_FILE_NAME}.json or ${CONFIG_FILE_NAME}.ts`);
|
|
90
91
|
return null;
|
|
91
92
|
});
|
|
92
93
|
}
|
|
@@ -115,9 +116,15 @@ function getEnvironmentType() {
|
|
|
115
116
|
function getHostProjectName() {
|
|
116
117
|
return __awaiter(this, void 0, void 0, function* () {
|
|
117
118
|
if (!hostProjectName) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
try {
|
|
120
|
+
const packageJson = yield import(packageJsonPath);
|
|
121
|
+
hostProjectName = packageJson.name || null;
|
|
122
|
+
console.log("[ANALYTICS] Fetched project name from package details: ", hostProjectName);
|
|
123
|
+
enabled = isEnabled();
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
console.warn("[ANALYTICS] Failed to fetch project name, continuing without");
|
|
127
|
+
}
|
|
121
128
|
}
|
|
122
129
|
return hostProjectName;
|
|
123
130
|
});
|
|
@@ -127,11 +134,12 @@ function enrichEventData(data) {
|
|
|
127
134
|
data.eventId = uuidv4();
|
|
128
135
|
data.client = hostProjectName;
|
|
129
136
|
data.sessionId = getSessionId();
|
|
137
|
+
data.url = window.location.href;
|
|
130
138
|
region && (data.region = region);
|
|
131
139
|
address && !data.address && (data.address = address);
|
|
132
140
|
coordinates && !data.coordinates && (data.coordinates = coordinates);
|
|
133
141
|
!data.timestamp && (data.timestamp = new Date());
|
|
134
|
-
!data.userId && (data.userId = user ? user.email : ar_anonymous_id);
|
|
142
|
+
!data.userId && (data.userId = (user ? user.email : ar_anonymous_id));
|
|
135
143
|
!data.email && user && user.email && (data.email = user.email);
|
|
136
144
|
user && (data.user = user);
|
|
137
145
|
}
|
|
@@ -148,7 +156,7 @@ export function sendBulkData(data, callback) {
|
|
|
148
156
|
function sendAnalyticsData(data) {
|
|
149
157
|
return __awaiter(this, void 0, void 0, function* () {
|
|
150
158
|
try {
|
|
151
|
-
if (!apiKey && !
|
|
159
|
+
if (!apiKey && !analyticsLogEndpoint) {
|
|
152
160
|
console.error('Neither of API key and Analytics server configured. Data not sent.');
|
|
153
161
|
return;
|
|
154
162
|
}
|
|
@@ -162,7 +170,7 @@ function sendAnalyticsData(data) {
|
|
|
162
170
|
if (apiKey) {
|
|
163
171
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
164
172
|
}
|
|
165
|
-
const logEndpoint = apiKey ? ARMCO_SERVER + ADD :
|
|
173
|
+
const logEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsLogEndpoint;
|
|
166
174
|
try {
|
|
167
175
|
const response = yield fetch(logEndpoint, options);
|
|
168
176
|
console.log('Analytics data sent to server:', logEndpoint, data, response.status, response.statusText);
|
|
@@ -179,7 +187,7 @@ function sendAnalyticsData(data) {
|
|
|
179
187
|
function tagEvents(email) {
|
|
180
188
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
189
|
try {
|
|
182
|
-
if (!apiKey && !
|
|
190
|
+
if (!apiKey && !analyticsTagEndpoint) {
|
|
183
191
|
console.error('Neither of API key and Analytics server configured. Tagging won\'t be attempted.');
|
|
184
192
|
return;
|
|
185
193
|
}
|
|
@@ -193,7 +201,7 @@ function tagEvents(email) {
|
|
|
193
201
|
if (apiKey) {
|
|
194
202
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
195
203
|
}
|
|
196
|
-
const tagEndpoint = apiKey ? ARMCO_SERVER + ADD :
|
|
204
|
+
const tagEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsTagEndpoint;
|
|
197
205
|
const response = yield fetch(tagEndpoint, options);
|
|
198
206
|
console.log('Analytics data sent to server:', tagEndpoint, response.status, response.statusText);
|
|
199
207
|
}
|
|
@@ -316,22 +324,14 @@ function handleTrackedItemClick(e) {
|
|
|
316
324
|
const value = 'value' in clickedElement && clickedElement.value
|
|
317
325
|
? clickedElement.value
|
|
318
326
|
: null;
|
|
319
|
-
const mergedData = Object.assign({}, dataAttributes, {
|
|
320
|
-
id,
|
|
327
|
+
const mergedData = Object.assign(Object.assign({}, dataAttributes), { id,
|
|
321
328
|
name,
|
|
322
329
|
classes,
|
|
323
330
|
textContent,
|
|
324
|
-
href,
|
|
325
|
-
tagName: elementType,
|
|
326
|
-
role,
|
|
331
|
+
href, tagName: elementType, role,
|
|
327
332
|
parentElementId,
|
|
328
|
-
|
|
329
|
-
trackEvent("CLICK", {
|
|
330
|
-
click: {
|
|
331
|
-
data: mergedData,
|
|
332
|
-
value,
|
|
333
|
-
},
|
|
334
|
-
});
|
|
333
|
+
value });
|
|
334
|
+
trackEvent("CLICK", { element: mergedData });
|
|
335
335
|
}
|
|
336
336
|
}
|
|
337
337
|
function hookTrackers() {
|
|
@@ -429,7 +429,8 @@ function loadAnalytics(config) {
|
|
|
429
429
|
if (CONFIG) {
|
|
430
430
|
console.log("[ANALYTICS] Configuration loaded");
|
|
431
431
|
apiKey = CONFIG.apiKey || null;
|
|
432
|
-
|
|
432
|
+
analyticsLogEndpoint = CONFIG.analyticsLogEndpoint || null;
|
|
433
|
+
analyticsTagEndpoint = CONFIG.analyticsTagEndpoint || null;
|
|
433
434
|
hostProjectName = CONFIG.hostProjectName || hostProjectName || null;
|
|
434
435
|
console.log("[ANALYTICS] Check if Analytics enabled");
|
|
435
436
|
enabled = isEnabled();
|
|
@@ -446,6 +447,14 @@ function loadAnalytics(config) {
|
|
|
446
447
|
const anonId = generateAnonymousId();
|
|
447
448
|
console.log("Tracking User as", anonId);
|
|
448
449
|
startSession();
|
|
450
|
+
window.addEventListener('load', function () {
|
|
451
|
+
console.log("[ANALYTICS] Logging page load");
|
|
452
|
+
trackEvent("PAGE");
|
|
453
|
+
});
|
|
454
|
+
window.addEventListener("popstate", (event) => {
|
|
455
|
+
const url = window.location.href;
|
|
456
|
+
trackEvent("NAV", { url });
|
|
457
|
+
});
|
|
449
458
|
}
|
|
450
459
|
else {
|
|
451
460
|
console.warn("[ANALYTICS] Analytics blocked by client, or disabled by configuration!");
|
package/constants.d.ts
CHANGED
package/constants.js
CHANGED
package/index.interface.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export interface Event {
|
|
|
14
14
|
}
|
|
15
15
|
export interface ConfigType {
|
|
16
16
|
apiKey?: string;
|
|
17
|
-
|
|
17
|
+
analyticsLogEndpoint?: string;
|
|
18
|
+
analyticsTagEndpoint?: string;
|
|
18
19
|
hostProjectName?: string;
|
|
19
20
|
trackEvents?: Array<string>;
|
|
20
21
|
submissionStrategy?: SubmissionStrategy;
|