@armco/analytics 0.2.1 → 0.2.3
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 +75 -37
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -163,8 +163,13 @@ function sendAnalyticsData(data) {
|
|
|
163
163
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
164
164
|
}
|
|
165
165
|
const logEndpoint = apiKey ? ARMCO_SERVER + ADD : analyticsEndpoint;
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
try {
|
|
167
|
+
const response = yield fetch(logEndpoint, options);
|
|
168
|
+
console.log('Analytics data sent to server:', logEndpoint, data, response.status, response.statusText);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
console.warn("Failed attempt to log event");
|
|
172
|
+
}
|
|
168
173
|
}
|
|
169
174
|
catch (error) {
|
|
170
175
|
console.error('Failed to send analytics data:', error);
|
|
@@ -264,6 +269,71 @@ function populateLocationDetails() {
|
|
|
264
269
|
}
|
|
265
270
|
region = localTimeRegion;
|
|
266
271
|
}
|
|
272
|
+
const trackedItems = [
|
|
273
|
+
"a[href]",
|
|
274
|
+
"button",
|
|
275
|
+
"input[type='button']",
|
|
276
|
+
"input[type='submit']",
|
|
277
|
+
"input[type='reset']",
|
|
278
|
+
"input[type='checkbox']",
|
|
279
|
+
"input[type='radio']",
|
|
280
|
+
"select",
|
|
281
|
+
"textarea",
|
|
282
|
+
"area",
|
|
283
|
+
"details",
|
|
284
|
+
"summary",
|
|
285
|
+
"iframe",
|
|
286
|
+
"object",
|
|
287
|
+
"embed",
|
|
288
|
+
"label",
|
|
289
|
+
"img",
|
|
290
|
+
"[role='button']",
|
|
291
|
+
"[role='checkbox']",
|
|
292
|
+
"[role='link']",
|
|
293
|
+
"[role='menuitem']",
|
|
294
|
+
"[role='menuitemcheckbox']",
|
|
295
|
+
"[role='menuitemradio']",
|
|
296
|
+
"[data-track='true']"
|
|
297
|
+
];
|
|
298
|
+
function isClickable(element) {
|
|
299
|
+
return element.matches(trackedItems.join(", ")) ||
|
|
300
|
+
element.onclick != null ||
|
|
301
|
+
window.getComputedStyle(element).cursor == "pointer";
|
|
302
|
+
}
|
|
303
|
+
function handleTrackedItemClick(e) {
|
|
304
|
+
var _a;
|
|
305
|
+
const clickedElement = e.target;
|
|
306
|
+
if (isClickable(clickedElement)) {
|
|
307
|
+
const dataAttributes = Object.assign({}, clickedElement.dataset);
|
|
308
|
+
const id = clickedElement.id || null;
|
|
309
|
+
const name = clickedElement.getAttribute('name') || null;
|
|
310
|
+
const classes = Array.from(clickedElement.classList);
|
|
311
|
+
const elementType = clickedElement.tagName.toLowerCase();
|
|
312
|
+
const textContent = clickedElement.textContent;
|
|
313
|
+
const href = 'href' in clickedElement ? clickedElement.href : null;
|
|
314
|
+
const role = clickedElement.getAttribute('role') || null;
|
|
315
|
+
const parentElementId = ((_a = clickedElement.parentElement) === null || _a === void 0 ? void 0 : _a.id) || null;
|
|
316
|
+
const value = 'value' in clickedElement && clickedElement.value
|
|
317
|
+
? clickedElement.value
|
|
318
|
+
: null;
|
|
319
|
+
const mergedData = Object.assign({}, dataAttributes, {
|
|
320
|
+
id,
|
|
321
|
+
name,
|
|
322
|
+
classes,
|
|
323
|
+
textContent,
|
|
324
|
+
href,
|
|
325
|
+
tagName: elementType,
|
|
326
|
+
role,
|
|
327
|
+
parentElementId,
|
|
328
|
+
});
|
|
329
|
+
trackEvent("CLICK", {
|
|
330
|
+
click: {
|
|
331
|
+
data: mergedData,
|
|
332
|
+
value,
|
|
333
|
+
},
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
267
337
|
function hookTrackers() {
|
|
268
338
|
if (environment === "browser") {
|
|
269
339
|
const TRACK_EVENTS = (CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.trackEvents) || ["click", "submit", "select-change"];
|
|
@@ -271,41 +341,8 @@ function hookTrackers() {
|
|
|
271
341
|
console.log("[ANALYTICS] Attaching Click handlers");
|
|
272
342
|
const clickables = Array.from(document.querySelectorAll('*'));
|
|
273
343
|
console.log("[ANALYTICS] Found " + clickables.length + " items that can be clicked!");
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return element.tagName === "BUTTON" ||
|
|
277
|
-
element.tagName === "A" ||
|
|
278
|
-
element.onclick != null ||
|
|
279
|
-
window.getComputedStyle(element).cursor == "pointer";
|
|
280
|
-
})
|
|
281
|
-
.forEach((element) => {
|
|
282
|
-
element.addEventListener("click", e => {
|
|
283
|
-
var _a;
|
|
284
|
-
const clickedElement = e.target;
|
|
285
|
-
const id = clickedElement.id;
|
|
286
|
-
const classes = Array.from(clickedElement.classList);
|
|
287
|
-
const dataAttributes = Object.keys(clickedElement.dataset).map(key => ({ [key]: clickedElement.dataset[key] }));
|
|
288
|
-
const textContent = clickedElement.textContent;
|
|
289
|
-
const href = 'href' in clickedElement ? clickedElement.href : null;
|
|
290
|
-
const elementType = clickedElement.tagName.toLowerCase();
|
|
291
|
-
const parentElementId = ((_a = clickedElement.parentElement) === null || _a === void 0 ? void 0 : _a.id) || null;
|
|
292
|
-
const value = 'value' in clickedElement && clickedElement.value
|
|
293
|
-
? clickedElement.value
|
|
294
|
-
: null;
|
|
295
|
-
trackEvent("CLICK", {
|
|
296
|
-
click: {
|
|
297
|
-
id,
|
|
298
|
-
classes,
|
|
299
|
-
dataAttributes,
|
|
300
|
-
textContent,
|
|
301
|
-
href,
|
|
302
|
-
elementType,
|
|
303
|
-
parentElementId,
|
|
304
|
-
value,
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
});
|
|
344
|
+
console.log("[ANALYTICS] Dynamically added elements will be added to this list.");
|
|
345
|
+
document.addEventListener("click", handleTrackedItemClick);
|
|
309
346
|
console.log("[ANALYTICS] Click handlers Attached");
|
|
310
347
|
}
|
|
311
348
|
if (TRACK_EVENTS.indexOf("submit") > -1) {
|
|
@@ -422,6 +459,7 @@ function init(config) {
|
|
|
422
459
|
const projectName = config && config.hostProjectName;
|
|
423
460
|
if (projectName) {
|
|
424
461
|
hostProjectName = projectName;
|
|
462
|
+
loadAnalytics(config);
|
|
425
463
|
}
|
|
426
464
|
else {
|
|
427
465
|
getHostProjectName()
|