@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 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 analyticsEndpoint;
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
- const packageJson = yield import(packageJsonPath);
119
- hostProjectName = packageJson.name || null;
120
- enabled = isEnabled();
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 && !analyticsEndpoint) {
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 : analyticsEndpoint;
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 && !analyticsEndpoint) {
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 : analyticsEndpoint;
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
- analyticsEndpoint = CONFIG.analyticsEndpoint || null;
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
@@ -1,3 +1,3 @@
1
- export declare const ARMCO_SERVER = "http://telemetry.notabuck.com/api/events/";
1
+ export declare const ARMCO_SERVER = "https://telemetry.notabuck.com/events/";
2
2
  export declare const ADD = "add";
3
3
  export declare const TAG = "tag";
package/constants.js CHANGED
@@ -1,3 +1,3 @@
1
- export const ARMCO_SERVER = "http://telemetry.notabuck.com/api/events/";
1
+ export const ARMCO_SERVER = "https://telemetry.notabuck.com/events/";
2
2
  export const ADD = "add";
3
3
  export const TAG = "tag";
@@ -14,7 +14,8 @@ export interface Event {
14
14
  }
15
15
  export interface ConfigType {
16
16
  apiKey?: string;
17
- analyticsEndpoint?: string;
17
+ analyticsLogEndpoint?: string;
18
+ analyticsTagEndpoint?: string;
18
19
  hostProjectName?: string;
19
20
  trackEvents?: Array<string>;
20
21
  submissionStrategy?: SubmissionStrategy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armco/analytics",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Browser Based Analytics interceptor for configured events",
5
5
  "main": "index.js",
6
6
  "type": "module",