@armco/analytics 0.1.1 → 0.1.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.
@@ -1,7 +1,16 @@
1
1
  import { startSession, getSessionId } from "./session";
2
- import { User, Event } from "./index.d";
2
+ import { User, Event, SubmissionStrategy } from "./index.d";
3
+ interface ConfigType {
4
+ apiKey?: string;
5
+ analyticsEndpoint?: string;
6
+ hostProjectName?: string;
7
+ trackEvents?: Array<string>;
8
+ submissionStrategy?: SubmissionStrategy;
9
+ showPopUp?: boolean;
10
+ }
3
11
  declare let enabled: boolean | null;
4
12
  declare function loadConfiguration(): Promise<any>;
13
+ declare function getEnvironment(): string;
5
14
  declare function getEnvironmentType(): 'browser' | 'node' | 'unknown';
6
15
  export declare function sendBulkData(data: Event[], callback?: Function): Promise<void>;
7
16
  declare function trackEvent(event: string | Event, data?: any): void;
@@ -13,6 +22,6 @@ declare function enableTracking(enable: boolean): void;
13
22
  declare function generateAnonymousId(): string;
14
23
  declare function identify(appUser: User): void;
15
24
  declare function sendHostProjectName(): void;
16
- declare function init(): void;
25
+ declare function init(config?: ConfigType): void;
17
26
  declare function logout(): void;
18
- export { enabled as analyticsEnabled, init, identify, getEnvironmentType, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
27
+ export { enabled as analyticsEnabled, init, identify, getEnvironmentType, getEnvironment, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
package/dist/analytics.js CHANGED
@@ -28,6 +28,7 @@ const CONFIG_FILE_NAME = "analyticsrc";
28
28
  let region;
29
29
  let address;
30
30
  let coordinates;
31
+ let runtime = null;
31
32
  let CONFIG;
32
33
  let environment;
33
34
  function isTypeScriptProject() {
@@ -66,7 +67,7 @@ function isTypeScriptProject() {
66
67
  }
67
68
  function loadConfiguration() {
68
69
  return __awaiter(this, void 0, void 0, function* () {
69
- const ROOT = "../../../../";
70
+ const ROOT = (runtime || getEnvironment()) === "production" ? "../../" : "../../../../";
70
71
  let configFilePath = `${ROOT}${CONFIG_FILE_NAME}.json`;
71
72
  try {
72
73
  const config = yield import(configFilePath);
@@ -89,6 +90,15 @@ function loadConfiguration() {
89
90
  return null;
90
91
  });
91
92
  }
93
+ function getEnvironment() {
94
+ if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV) {
95
+ return process.env.NODE_ENV;
96
+ }
97
+ if (import.meta.env && import.meta.env.MODE) {
98
+ return import.meta.env.MODE;
99
+ }
100
+ return 'development';
101
+ }
92
102
  function getEnvironmentType() {
93
103
  if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
94
104
  return 'browser';
@@ -376,40 +386,61 @@ function showTrackingPopup() {
376
386
  $('.tracking-popup').remove();
377
387
  });
378
388
  }
379
- function init() {
389
+ function loadAnalytics(config) {
390
+ console.log("[ANALYTICS] Loading Configuration");
391
+ CONFIG = config;
392
+ if (CONFIG) {
393
+ console.log("[ANALYTICS] Configuration loaded");
394
+ apiKey = CONFIG.apiKey || null;
395
+ analyticsEndpoint = CONFIG.analyticsEndpoint || null;
396
+ hostProjectName = CONFIG.hostProjectName || hostProjectName || null;
397
+ console.log("[ANALYTICS] Check if Analytics enabled");
398
+ enabled = isEnabled();
399
+ if (enabled) {
400
+ console.log("[ANALYTICS] Display Tracker Popup");
401
+ CONFIG.showPopUp && showTrackingPopup();
402
+ console.log("[ANALYTICS] Hook Event Trackers");
403
+ hookTrackers();
404
+ console.log("[ANALYTICS] Hook Handlers to flush events (use when submissionStrategy is configured as \"DEFER\"");
405
+ hookFlushHandlers(CONFIG.submissionStrategy);
406
+ console.log("[ANALYTICS] Find User Location Details");
407
+ populateLocationDetails();
408
+ console.log("[ANALYTICS] Initiate Session and Anonymous User ID");
409
+ const anonId = generateAnonymousId();
410
+ console.log("Tracking User as", anonId);
411
+ startSession();
412
+ }
413
+ else {
414
+ console.warn("[ANALYTICS] Analytics blocked by client, or disabled by configuration!");
415
+ }
416
+ }
417
+ }
418
+ function init(config) {
380
419
  try {
381
420
  environment = getEnvironmentType();
382
- console.log("[ANALYTICS] Loading Configuration");
383
- console.log("[ANALYTICS] Environment: ", import.meta.env.MODE);
384
- loadConfiguration()
385
- .then((config) => __awaiter(this, void 0, void 0, function* () {
386
- CONFIG = config;
387
- if (CONFIG) {
388
- console.log("[ANALYTICS] Configuration loaded");
389
- apiKey = CONFIG.apiKey || null;
390
- analyticsEndpoint = CONFIG.analyticsEndpoint || null;
391
- hostProjectName = CONFIG.hostProjectName || (yield getHostProjectName()) || null;
392
- console.log("[ANALYTICS] Check if Analytics enabled");
393
- enabled = isEnabled();
394
- if (enabled) {
395
- console.log("[ANALYTICS] Display Tracker Popup");
396
- CONFIG.showPopUp && showTrackingPopup();
397
- console.log("[ANALYTICS] Hook Event Trackers");
398
- hookTrackers();
399
- console.log("[ANALYTICS] Hook Handlers to flush events (use when submissionStrategy is configured as \"DEFER\"");
400
- hookFlushHandlers(CONFIG.submissionStrategy);
401
- console.log("[ANALYTICS] Find User Location Details");
402
- populateLocationDetails();
403
- console.log("[ANALYTICS] Initiate Session and Anonymous User ID");
404
- const anonId = generateAnonymousId();
405
- console.log("Tracking User as", anonId);
406
- startSession();
421
+ runtime = getEnvironment();
422
+ const projectName = config && config.hostProjectName;
423
+ if (projectName) {
424
+ hostProjectName = projectName;
425
+ }
426
+ else {
427
+ getHostProjectName()
428
+ .then((projectName) => {
429
+ hostProjectName = projectName;
430
+ if (config) {
431
+ loadAnalytics(config);
407
432
  }
408
433
  else {
409
- console.warn("[ANALYTICS] Analytics blocked by client, or disabled by configuration!");
434
+ loadConfiguration()
435
+ .then(config => {
436
+ loadAnalytics(config);
437
+ });
410
438
  }
411
- }
412
- }));
439
+ })
440
+ .catch((err) => {
441
+ console.error("[ANALYTICS] Couldn't determine host project name, no events will be logged!");
442
+ });
443
+ }
413
444
  }
414
445
  catch (e) {
415
446
  console.log("[ANALYTICS]", e);
@@ -422,4 +453,4 @@ function logout() {
422
453
  }
423
454
  terminateSession();
424
455
  }
425
- export { enabled as analyticsEnabled, init, identify, getEnvironmentType, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
456
+ export { enabled as analyticsEnabled, init, identify, getEnvironmentType, getEnvironment, loadConfiguration, logout, trackEvent, trackPageView, trackError, enableTracking, generateAnonymousId, sendHostProjectName, getSessionId, startSession, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armco/analytics",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Browser Based Analytics interceptor for configured events",
5
5
  "main": "index.js",
6
6
  "type": "module",