@@ -4,7 +4,14 @@ import root from 'window-or-global';
4
4
* @constant {boolean} scrollTracker determines whether scroll tracking analytics is enabled
5
5
* @private
6
6
*/
7
- const scrollTracker = process . env . SCROLL_TRACKING === 'true' || false ;
7
+ const _scrollTracker = process . env . SCROLL_TRACKING === 'true' || false ;
8
+
9
+ /**
10
+ * Current NODE_ENV
11
+ * @type {string | string }
12
+ * @private
13
+ */
14
+ const _env = process . env . NODE_ENV || 'development' ;
8
15
9
16
/**
10
17
* Analytics API class with methods for firing analytics events on
@@ -40,6 +47,15 @@ class AnalyticsAPI {
40
47
}
41
48
}
42
49
50
+ /**
51
+ * Initializes all analytics global tracking init functions
52
+ */
53
+ static initAll ( ) {
54
+ this . initScrollTracker ( ) ;
55
+ this . initDynamicTabs ( ) ;
56
+ this . initModals ( ) ;
57
+ }
58
+
43
59
/**
44
60
*
45
61
* If scroll tracking is enabled, this method will fire an event for every 400px
@@ -54,7 +70,7 @@ class AnalyticsAPI {
54
70
* }
55
71
**/
56
72
static initScrollTracker ( ) {
57
- if ( scrollTracker ) {
73
+ if ( _scrollTracker ) {
58
74
const trackingInterval = 400 ;
59
75
let trackedMarker = 0 ;
60
76
let curMarker = 0 ;
@@ -99,20 +115,102 @@ class AnalyticsAPI {
99
115
* }
100
116
*/
101
117
static initDynamicTabs ( ) {
102
- // const fireEvent = this.registerEvent;
103
- console . log ( 'init dynamic tabs' ) ;
118
+ const tabSelected = this . triggerTabSelected ;
119
+ root . document . addEventListener ( 'tab-selected' , function ( evt ) {
120
+ tabSelected ( evt . target . id , evt . detail . item . innerText ) ;
121
+ } ) ;
122
+ }
104
123
105
- root . addEventListener ( 'tab-selected' , function ( evt ) {
106
- console . log ( 'evt' , evt ) ;
107
- /*fireEvent({
124
+ /**
125
+ * Triggers to CLICK event for the dynamic tabs
126
+ *
127
+ * @param {string } executionPath Target ID
128
+ * @param {string } targetTitle Target innerText
129
+ */
130
+ static triggerTabSelected ( executionPath , targetTitle ) {
131
+ try {
132
+ this . registerEvent ( {
108
133
type : 'element' ,
109
134
primaryCategory : 'WIDGET' ,
110
135
eventName : 'CLICK' ,
111
136
eventCategoryGroup : 'TABS DYNAMIC' ,
112
- executionPath: $id,
113
- targetTitle: $text
114
- });*/
137
+ executionPath : executionPath ,
138
+ targetTitle : targetTitle ,
139
+ } ) ;
140
+ } catch ( err ) {
141
+ if ( _env !== 'production' ) {
142
+ console . error ( 'Error triggering tab event:' , err ) ;
143
+ }
144
+ }
145
+ }
146
+
147
+ /**
148
+ * This instantiates an event listener to trigger an event if the Carbon
149
+ * Modal component is being interacted with by the user
150
+ *
151
+ * @example
152
+ * import { AnalyticsAPI } from '@carbon/ibmdotcom-services';
153
+ *
154
+ * function init() {
155
+ * AnalyticsAPI.initModals();
156
+ * }
157
+ */
158
+ static initModals ( ) {
159
+ const modalHide = this . triggerModalHide ;
160
+ root . document . addEventListener ( 'modal-hidden' , function ( evt ) {
161
+ modalHide ( evt . target . id , evt . detail . launchingElement . innerText ) ;
115
162
} ) ;
163
+
164
+ const modalShow = this . triggerModalShow ;
165
+ root . document . addEventListener ( 'modal-shown' , function ( evt ) {
166
+ modalShow ( evt . target . id , evt . detail . launchingElement . innerText ) ;
167
+ } ) ;
168
+ }
169
+
170
+ /**
171
+ * Triggers the HIDE event for the modal
172
+ *
173
+ * @param {string } executionPath Target ID
174
+ * @param {string } targetTitle Target innerText
175
+ */
176
+ static triggerModalHide ( executionPath , targetTitle ) {
177
+ try {
178
+ this . registerEvent ( {
179
+ type : 'element' ,
180
+ primaryCategory : 'WIDGET' ,
181
+ eventName : 'HIDE' ,
182
+ eventCategoryGroup : 'SHOWHIDE' ,
183
+ executionPath : executionPath ,
184
+ targetTitle : targetTitle ,
185
+ } ) ;
186
+ } catch ( err ) {
187
+ if ( _env !== 'production' ) {
188
+ console . error ( 'Error triggering modal hide event:' , err ) ;
189
+ }
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Triggers the SHOW event for the modal
195
+ *
196
+ * @param {string } executionPath Target ID
197
+ * @param {string } targetTitle Target innerText
198
+ */
199
+ static triggerModalShow ( executionPath , targetTitle ) {
200
+ try {
201
+ this . registerEvent ( {
202
+ type : 'element' ,
203
+ primaryCategory : 'WIDGET' ,
204
+ eventName : 'SHOW' ,
205
+ eventCategoryGroup : 'SHOWHIDE' ,
206
+ executionPath : executionPath ,
207
+ targetTitle : targetTitle ,
208
+ } ) ;
209
+ } catch ( err ) {
210
+ if ( _env !== 'production' ) {
211
+ console . error ( 'Error triggering modal show event:' , err ) ;
212
+ }
213
+ }
116
214
}
117
215
}
118
216
0 commit comments