Skip to content

Commit 1dade0e

Browse files
committed
feat(analytics): global analytics for modal and dynamic tabs
1 parent 6ad3085 commit 1dade0e

18 files changed

+183
-113
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

packages/react/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
},
4949
"dependencies": {
5050
"@babel/runtime": "^7.5.5",
51-
"@carbon/ibmdotcom-services": "1.1.0-alpha.743",
51+
"@carbon/ibmdotcom-services": "1.1.0-alpha.762",
5252
"@carbon/ibmdotcom-styles": "1.0.0",
53-
"@carbon/ibmdotcom-utilities": "1.1.0-alpha.743",
53+
"@carbon/ibmdotcom-utilities": "1.1.0-alpha.762",
5454
"autosuggest-highlight": "^3.1.1",
5555
"classnames": "2.2.6",
5656
"react-autosuggest": "^9.4.3",

packages/react/src/components/ButtonGroup/__stories__/Tabs.stories.js

-82
This file was deleted.

packages/react/src/components/Footer/Footer.js

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
settings as ddsSettings,
1212
ipcinfoCookie,
1313
} from '@carbon/ibmdotcom-utilities';
14+
import { globalInit } from '@carbon/ibmdotcom-utilities';
1415
import { settings } from 'carbon-components';
1516
import { TranslationAPI } from '@carbon/ibmdotcom-services';
1617
import classNames from 'classnames';
@@ -32,6 +33,11 @@ const Footer = ({ type }) => {
3233
const [footerMenuData, setFooterMenuData] = useState([]);
3334
const [footerLegalData, setFooterLegalData] = useState([]);
3435

36+
useEffect(() => {
37+
// initialize global execution calls
38+
globalInit();
39+
}, []);
40+
3541
useEffect(() => {
3642
(async () => {
3743
const response = await TranslationAPI.getTranslation();

packages/react/src/components/Footer/__tests__/Footer.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ jest.mock('@carbon/ibmdotcom-services', () => ({
2828
getLocale: jest.fn(() => Promise.resolve({ cc: 'us', lc: 'en' })),
2929
getList: jest.fn(() => Promise.resolve({})),
3030
},
31+
DDOAPI: {
32+
setVersion: jest.fn(),
33+
},
34+
AnalyticsAPI: {
35+
initAll: jest.fn(),
36+
},
3137
}));
3238

3339
describe('<Footer />', () => {

packages/react/src/components/Masthead/Masthead.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import PropTypes from 'prop-types';
1010
import { settings as ddsSettings } from '@carbon/ibmdotcom-utilities';
1111
import { settings } from 'carbon-components';
1212
import root from 'window-or-global';
13-
import { init } from '../../global';
13+
import { globalInit } from '@carbon/ibmdotcom-utilities';
1414
import { User20, UserOnline20 } from '@carbon/icons-react';
1515
import { IbmLogo } from '../Icon';
1616
import {
@@ -49,7 +49,7 @@ const Masthead = ({ navigation, hasProfile, hasSearch, ...mastheadProps }) => {
4949

5050
useEffect(() => {
5151
// initialize global execution calls
52-
init();
52+
globalInit();
5353
}, []);
5454

5555
useEffect(() => {

packages/services/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"jsdoc": "rimraf docs && jsdoc -c ./jsdoc.json ./README.md"
3535
},
3636
"dependencies": {
37-
"@carbon/ibmdotcom-utilities": "1.1.0-alpha.743",
37+
"@carbon/ibmdotcom-utilities": "1.1.0-alpha.762",
3838
"axios": "^0.19.0",
3939
"jsonp": "^0.2.1",
4040
"window-or-global": "^1.0.1"

packages/services/src/services/Analytics/Analytics.js

+108-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import root from 'window-or-global';
44
* @constant {boolean} scrollTracker determines whether scroll tracking analytics is enabled
55
* @private
66
*/
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';
815

916
/**
1017
* Analytics API class with methods for firing analytics events on
@@ -40,6 +47,15 @@ class AnalyticsAPI {
4047
}
4148
}
4249

50+
/**
51+
* Initializes all analytics global tracking init functions
52+
*/
53+
static initAll() {
54+
this.initScrollTracker();
55+
this.initDynamicTabs();
56+
this.initModals();
57+
}
58+
4359
/**
4460
*
4561
* If scroll tracking is enabled, this method will fire an event for every 400px
@@ -54,7 +70,7 @@ class AnalyticsAPI {
5470
* }
5571
**/
5672
static initScrollTracker() {
57-
if (scrollTracker) {
73+
if (_scrollTracker) {
5874
const trackingInterval = 400;
5975
let trackedMarker = 0;
6076
let curMarker = 0;
@@ -99,20 +115,102 @@ class AnalyticsAPI {
99115
* }
100116
*/
101117
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+
}
104123

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({
108133
type: 'element',
109134
primaryCategory: 'WIDGET',
110135
eventName: 'CLICK',
111136
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);
115162
});
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+
}
116214
}
117215
}
118216

packages/utilities/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"jsdoc": "rimraf docs && jsdoc -c ./jsdoc.json ./README.md"
3535
},
3636
"dependencies": {
37+
"@carbon/ibmdotcom-services": "1.1.0-alpha.763",
3738
"axios": "^0.19.0",
3839
"js-cookie": "^2.2.1",
3940
"window-or-global": "^1.0.1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { globalInit } from '../';
2+
import { DDOAPI, AnalyticsAPI } from '@carbon/ibmdotcom-services';
3+
4+
jest.mock('@carbon/ibmdotcom-services', () => ({
5+
DDOAPI: {
6+
setVersion: jest.fn(),
7+
},
8+
AnalyticsAPI: {
9+
initAll: jest.fn(),
10+
},
11+
}));
12+
13+
describe('Global init function', () => {
14+
it('should execute the DDO settings', () => {
15+
globalInit();
16+
expect(DDOAPI.setVersion).toHaveBeenCalled();
17+
});
18+
19+
it('should execute the AnalyticsAPI initAll', () => {
20+
globalInit();
21+
expect(AnalyticsAPI.initAll).toHaveBeenCalled();
22+
});
23+
});

packages/react/src/global.js packages/utilities/src/utilities/global/global.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let _initialized = false;
1212
/**
1313
* Initializes various global functions
1414
*/
15-
export function init() {
15+
export function globalInit() {
1616
if (_initialized) {
1717
return;
1818
} else {
@@ -23,6 +23,5 @@ export function init() {
2323
DDOAPI.setVersion();
2424

2525
// analytics tracking
26-
AnalyticsAPI.initScrollTracker();
27-
AnalyticsAPI.initDynamicTabs();
26+
AnalyticsAPI.initAll();
2827
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export * from './global';

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
export * from './escaperegexp';
99
export * from './featureflag';
1010
export * from './geolocation';
11+
export * from './global';
1112
export * from './ipcinfoCookie';
1213
export * from './serialize';
1314
export * from './settings';

0 commit comments

Comments
 (0)