@capillarytech/cap-ui-utils 1.4.1 → 1.4.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/index.js CHANGED
@@ -5,3 +5,4 @@ export { default as utilsSessionStorageApi } from "./utils/utilsSessionStorageAp
5
5
  export { default as dateHelper } from "./utils/date/dateHelper";
6
6
  export { default as formatter } from "./utils/formatter";
7
7
  export { default as validationHelper } from "./utils/validationHelper";
8
+ export { default as GTMTracker } from './utils/gtmTracker';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,79 @@
1
+ import GA from '../GA';
2
+ import { find, isEmpty } from 'lodash';
3
+ import log from './console/log';
4
+
5
+ let _instance = null;
6
+
7
+ export default class GTMTracker {
8
+
9
+ constructor (trackingId, appName, userData = {}) {
10
+ const userGtmData = this.getUserGtmData(userData);
11
+ const userUniqueId = `${userGtmData.userEmail}__${userGtmData.userId}`;
12
+ GA.initialize({
13
+ accessKey: trackingId,
14
+ trackUIError: true,
15
+ trackLoadPerformance: appName,
16
+ gaOptions: {
17
+ userId: btoa(userUniqueId),
18
+ },
19
+ });
20
+ GA.setCustomDimension({ userId: userUniqueId });
21
+ this.push('userAuthenticated', {
22
+ userData: userGtmData,
23
+ });
24
+ }
25
+
26
+ push = (eventType, eventObject) => window.dataLayer.push({ ...eventObject, event: eventType })
27
+
28
+ getUserGtmData = (userDetails = {}) => {
29
+ try {
30
+ const { user: userData, orgID, currentOrgDetails } = userDetails;
31
+ const {
32
+ basic_details: {
33
+ base_country: orgBaseCountry = '',
34
+ base_currency: orgBaseCurrency = '',
35
+ base_language: orgBaseLanguage = '',
36
+ } = {},
37
+ } = currentOrgDetails || {};
38
+ if (!isEmpty(userData)) {
39
+ const {
40
+ attributes: { USERNAME: userName, EMAIL: userEmail } = {},
41
+ proxyOrgList = [],
42
+ isCapUser,
43
+ refID,
44
+ orgName: userOrgName,
45
+ type: userRole = '',
46
+ } = userData;
47
+ const { orgName } = find(proxyOrgList, { orgID }) || {
48
+ orgName: userOrgName,
49
+ };
50
+ const gtmData = {
51
+ isCapUser,
52
+ orgId: orgID,
53
+ orgName,
54
+ userId: refID,
55
+ userType: userRole,
56
+ userName: userName?.value,
57
+ userEmail: userEmail?.value,
58
+ userRole,
59
+ orgBaseCountry,
60
+ orgBaseCurrency,
61
+ orgBaseLanguage,
62
+ domainName: userEmail?.value?.split('@').pop(),
63
+ };
64
+ return gtmData;
65
+ }
66
+ }
67
+ catch(e) {
68
+ log(e);
69
+ }
70
+ return {};
71
+ };
72
+ }
73
+
74
+ GTMTracker.getInstance = (trackingId, appName, userDetails) => {
75
+ if (_instance === null) {
76
+ _instance = new GTMTracker(trackingId, appName, userDetails);
77
+ }
78
+ return _instance;
79
+ };