@capillarytech/cap-ui-utils 1.2.3 → 1.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.
Files changed (2) hide show
  1. package/GA/index.js +89 -35
  2. package/package.json +1 -1
package/GA/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import ReactGA from 'react-ga';
2
- // import ttiPolyfill from 'tti-polyfill';
2
+ import ttiPolyfill from 'tti-polyfill';
3
+
3
4
  import timeTracker from './utils/timeTracker';
4
5
  import tracker from './utils/tracker';
5
6
  import routeTrackerWrapper from './utils/routeTrackerWrapper';
@@ -9,22 +10,74 @@ import warn from '../utils/console/warn';
9
10
  import log from '../utils/console/log';
10
11
 
11
12
  const _initialize = (GAConfigs) => {
12
- if (!GAConfigs.accessKey) {
13
+ const { accessKey, trackUIError, trackLoadPerformance } = GAConfigs || {}
14
+ if (!accessKey) {
13
15
  warn("Cannot initialize GA without accessKey");
14
16
  return;
17
+ } else {
18
+ ReactGA.initialize(accessKey,{debug: true,});
15
19
  }
16
- if(GAConfigs.accessKey) {
17
- ReactGA.initialize(GAConfigs.accessKey,{debug: true,});
18
- }
19
- if(GAConfigs.trackUIError){
20
+ if(trackUIError){
20
21
  errorTracks();
21
22
  }
23
+ if(trackLoadPerformance){
24
+ loadPerformanceTracking(trackLoadPerformance);
25
+ }
22
26
  eventsAfterInitialize();
23
27
  log("Cap GA initialized");
24
28
  }
25
29
  const setCustomDimension= value => {
26
30
  ReactGA.set(value);
27
31
  }
32
+ const loadPerformanceTracking = module =>{
33
+ //requestStart: when the browser issues a request to get the webpage from the server
34
+ // responsesStart: when the first byte of the website arrives
35
+ // responseEnd: When the last byte of the website arrives
36
+
37
+ const callback = list => {
38
+ list.getEntries().forEach(entry => {
39
+ const { responseStart = 0, requestStart = 0, responseEnd = 0 } = entry || {};
40
+ const serverLatency = Math.round((responseStart - requestStart).toFixed(2));
41
+ const downloadTime = Math.round((responseEnd - responseStart).toFixed(2));
42
+ const loadTime = Math.round((responseEnd - requestStart).toFixed(2));
43
+ // Server latency:
44
+ ReactGA.timing({
45
+ category: "performance- server latentcy",
46
+ variable: module,
47
+ value: serverLatency,
48
+ })
49
+ // Download time:
50
+ ReactGA.timing({
51
+ category: "performance- download time",
52
+ variable: module,
53
+ value: downloadTime
54
+ })
55
+ // Total app load time:
56
+ ReactGA.timing({
57
+ category: "performance- load time",
58
+ variable: module,
59
+ value: loadTime
60
+ })
61
+ });
62
+ };
63
+
64
+ try {
65
+ var observer = new PerformanceObserver(callback);
66
+ observer.observe({ entryTypes: ['navigation'] });
67
+ // Time to Interactive metric is essentially how long it takes for a user to be able to interact with your site.
68
+ // Time to Interactive is defined as the point at which the layout has stabilized, key webfonts are visible,
69
+ // and the main thread is available enough to handle user input.
70
+ ttiPolyfill.getFirstConsistentlyInteractive().then((tti) => {
71
+ ReactGA.timing({
72
+ category: "Time to Interactive",
73
+ variable: module,
74
+ value: Math.round(tti.toFixed(2))
75
+ })
76
+ });
77
+ }catch(e){
78
+ console.log(e);
79
+ }
80
+ }
28
81
  const initialize = (GAConfigs) =>{
29
82
  if (isEmpty(GAConfigs)) {
30
83
  warn("Cannot initialize empty object");
@@ -58,8 +111,9 @@ const errorTracks = () => {
58
111
  const message = [
59
112
  'Message: ' + msg,
60
113
  'URL: ' + url,
61
- 'Column: ' + columnNo,
62
- 'Error object: ' + JSON.stringify(error)
114
+ 'LineNo: ' + lineNo,
115
+ 'ColumnNo: ' + columnNo,
116
+ 'Error object: ' + JSON.stringify(error),
63
117
  ].join(' - ');
64
118
  tracker.trackException({
65
119
  description: message,
@@ -67,39 +121,39 @@ const errorTracks = () => {
67
121
  });
68
122
  };
69
123
  }
124
+ const getCallerName = () => {
125
+ let nameTrace;
126
+ try {
127
+ throw new Error();
128
+ } catch (e) {
129
+ // Error.prototype.stack Manually throw an error and catch the function name by tracing the stack
130
+ //As per MDN, the use of Error.prototype.stack is discouraged because of it's non-standard behaviour and it's behaviour might change on different browsers
131
+ //once we find better solution we will make the changes
132
+ let stackLines = e.stack.split('\n');
133
+ let callerIndex = 0;
134
+ for (let i in stackLines) {
135
+ if (stackLines.hasOwnProperty(i)) {
136
+ if (!stackLines[i].match(/http[s]?:\/\//)) continue;
137
+ callerIndex = Number(i) + 2;
138
+ break;
139
+ }
140
+ }
141
+ nameTrace = stackLines[callerIndex].match(/([^(]+)@|at ([^(]+) \(/);
142
+ if (nameTrace === null) {
143
+ callerIndex = callerIndex - 2;
144
+ nameTrace = stackLines[callerIndex].match(/([^(]+)@|at ([^(]+) \(/);
145
+ }
146
+ }
147
+ return nameTrace !== null ? nameTrace[1] || nameTrace[2] : '';
148
+ }
70
149
 
71
- // ttiPolyfill.getFirstConsistentlyInteractive().then(tti => {
72
- // console.log('performance tti', tti);
73
- // });
74
-
75
- // const callback = list => {
76
- // list.getEntries().forEach(entry => {
77
- // console.log('performance callback entry', entry);
78
- // console.log(
79
- // 'performance callback server latentcy',
80
- // entry.responseStart - entry.requestStart,
81
- // );
82
- // console.log(
83
- // 'performance callback download time',
84
- // entry.responseEnd - entry.responseStart,
85
- // );
86
- // console.log(
87
- // 'performance callback load time',
88
- // entry.responseEnd - entry.requestStart,
89
- // );
90
- // });
91
- // };
92
150
 
93
- // var observer = new PerformanceObserver(callback);
94
- // observer.observe({ entryTypes: ['navigation'] });
95
- // var perfData = window.performance.timing;
96
- // var pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;
97
- // console.log('Performance metrics', perfData, pageLoadTime);
98
151
 
99
152
  export default{
100
153
  initialize,
101
154
  timeTracker,
102
155
  tracker,
103
156
  routeTrackerWrapper,
104
- setCustomDimension
157
+ setCustomDimension,
158
+ getCallerName,
105
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {