Skip to content

Commit 558faaf

Browse files
authored
fix(geolocation): remove geolocation utility, replace with new DDO service method - getLocation (#10547)
### Related Ticket(s) [GeoLocation service]: API endpoint has been retired, switch to DDO #10546 ### Description As the API endpoint we were using before to detect user's location (country code) has been retired by the webmaster team, new method is to grab from the DDO (`digitalData.user.location`) populated by the IBM analytics script. This PR adds a new method the DDO service, which grabs the location from the DDO in the page and removes all instances of the geolocation utility which no longer works. ### Changelog **New** - `getLocation()` method under the DDO service - have the `Locale` service use the new DDO `getLocation` method instead **Removed** - instances of the `geolocation` utility <!-- React and Web Component deploy previews are enabled by default. --> <!-- To enable additional available deploy previews, apply the following --> <!-- labels for the corresponding package: --> <!-- *** "test: e2e": Codesandbox examples and e2e integration tests --> <!-- *** "package: services": Services --> <!-- *** "package: utilities": Utilities --> <!-- *** "RTL": React / Web Components (RTL) --> <!-- *** "feature flag": React / Web Components (experimental) -->
1 parent 7f4aaef commit 558faaf

File tree

12 files changed

+71
-128
lines changed

12 files changed

+71
-128
lines changed

packages/react/.env.example

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ SEARCH_TYPEAHEAD_API=<host for ibm.com search, e.g. https://www-api.ibm.com>
1313
MARKETING_SEARCH_VERSION=<api version for ibm.com marketing search, e.g. v3>
1414
MARKETING_SEARCH_HOST=<host for ibm.com marketing search, e.g. https://www.ibm.com>
1515

16-
# Geolocation
17-
GEO_API=<endpoint for getting user country by geolocation>
18-
1916
# VideoPlayer
2017
KALTURA_PARTNER_ID=<Kaltura partner id, e.g. 243342>
2118
KALTURA_UICONF_ID=<Kaltura uiconf id, e.g. 12905712>

packages/services/src/services/DDO/DDO.js

+13
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ class DDOAPI {
132132
return root.digitalData.page.pageInfo.language;
133133
});
134134
}
135+
136+
/**
137+
* Gets the country location of the user based on data populated by IBM analytics script.
138+
* Application should `window.digitalData` up-front, e.g. in a `<script>` tag in `<head>`.
139+
* For quick developerment purpose, what `ibm-common.js` automatically populates can be used.
140+
*
141+
* @returns {Promise<*>} Promise object
142+
*/
143+
static async getLocation() {
144+
return await this.isReady().then(() => {
145+
return root.digitalData.user.location.country.toLowerCase();
146+
});
147+
}
135148
}
136149

137150
export default DDOAPI;

packages/services/src/services/DDO/__tests__/DDO.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ describe('DDOAPI', () => {
3939
expect(root.digitalData.page.pageInfo.version).toEqual('dds.v1.0.0');
4040
});
4141

42+
it('should return the country location', async () => {
43+
await DDOAPI.getLocation();
44+
45+
expect(root.digitalData.user.location.country).toEqual('US');
46+
});
47+
4248
it('should set a loop if the data layer is not ready', async () => {
4349
root.digitalData.page.isDataLayerReady = false;
4450
jest.useFakeTimers();

packages/services/src/services/DDO/__tests__/data/response.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
{
22
"page": {
33
"attributes": {
4-
"pageidQueryStrings": [
5-
"q",
6-
"s",
7-
"tabType[0]",
8-
"tabType%5B0%5D"
9-
],
4+
"pageidQueryStrings": ["q", "s", "tabType[0]", "tabType%5B0%5D"],
105
"agentMobileOS": "unknown"
116
},
127
"category": {
@@ -156,6 +151,11 @@
156151
"isCountryMemberOfEU": false,
157152
"isCountryRequiringExplicitConsent": false
158153
},
154+
"location": {
155+
"country": "US",
156+
"stateProvince": "NJ",
157+
"city": "NEWARK"
158+
},
159159
"profile": {
160160
"dw": {},
161161
"ex": {},

packages/services/src/services/Locale/Locale.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import axios from 'axios';
9-
import geolocation from '../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/geolocation/geolocation';
9+
import { DDOAPI } from '../DDO';
1010
import ipcinfoCookie from '../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/ipcinfoCookie/ipcinfoCookie';
1111
import root from 'window-or-global';
1212

@@ -173,7 +173,7 @@ class LocaleAPI {
173173
*
174174
* Grab the locale from the `lang` attribute from html, else
175175
* check if ipcinfo cookie exists (ipcinfoCookie util)
176-
* if not, retrieve the user's locale through geolocation util + gets user's
176+
* if not, retrieve the user's locale through DDO service + gets user's
177177
* browser language preference then set the cookie
178178
*
179179
* @returns {object} object with lc and cc
@@ -197,7 +197,7 @@ class LocaleAPI {
197197
await this.getList(cookie);
198198
return cookie;
199199
} else {
200-
const cc = await geolocation();
200+
const cc = await DDOAPI.getLocation();
201201
/**
202202
* get language preference from browser
203203
* can return in either 'en-US' format or 'en' so will need to extract language only

packages/services/src/services/Locale/__tests__/Locale.test.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
* This source code is licensed under the Apache-2.0 license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
import {
8-
geolocation,
9-
ipcinfoCookie,
10-
} from '../../../internal/vendor/@carbon/ibmdotcom-utilities';
7+
import { ipcinfoCookie } from '../../../internal/vendor/@carbon/ibmdotcom-utilities';
118
import digitalDataResponse from '../../DDO/__tests__/data/response.json';
129
import LocaleAPI from '../Locale';
10+
import { DDOAPI } from '../../DDO';
1311
import mockAxios from 'axios';
1412
import oldSession from './data/timestamp_response.json';
1513
import response from './data/response.json';
@@ -25,10 +23,11 @@ jest.mock(
2523
})
2624
);
2725

28-
jest.mock(
29-
'../../../internal/vendor/@carbon/ibmdotcom-utilities/utilities/geolocation/geolocation',
30-
() => jest.fn(() => Promise.resolve('us'))
31-
);
26+
jest.mock('../../DDO', () => ({
27+
DDOAPI: {
28+
getLocation: jest.fn(() => Promise.resolve('us')),
29+
},
30+
}));
3231

3332
describe('LocaleAPI', () => {
3433
const handles = [];
@@ -445,33 +444,33 @@ describe('LocaleAPI', () => {
445444
expect(locale).toEqual({ cc: 'us', lc: 'en' });
446445
});
447446

448-
it('should get locale from geolocation on missing cookie', async () => {
447+
it('should get locale from DDO on missing cookie', async () => {
449448
ipcinfoCookie.get.mockImplementation(() => false);
450449

451450
await LocaleAPI.getLocale();
452451

453-
expect(geolocation).toHaveBeenCalledTimes(1);
452+
expect(DDOAPI.getLocation).toHaveBeenCalledTimes(1);
454453
});
455454

456-
it('should get locale from geolocation on missing cookie lc', async () => {
457-
geolocation.mockClear();
455+
it('should get locale from DDO on missing cookie lc', async () => {
456+
DDOAPI.getLocation.mockClear();
458457
ipcinfoCookie.get.mockImplementation(() => ({ cc: 'testCC' }));
459458

460459
await LocaleAPI.getLocale();
461460

462-
expect(geolocation).toHaveBeenCalledTimes(1);
461+
expect(DDOAPI.getLocation).toHaveBeenCalledTimes(1);
463462
});
464463

465-
it('should get locale from geolocation on missing cookie cc', async () => {
466-
geolocation.mockClear();
464+
it('should get locale from DDO on missing cookie cc', async () => {
465+
DDOAPI.getLocation.mockClear();
467466
ipcinfoCookie.get.mockImplementation(() => ({ lc: 'testLC' }));
468467

469468
await LocaleAPI.getLocale();
470469

471-
expect(geolocation).toHaveBeenCalledTimes(1);
470+
expect(DDOAPI.getLocation).toHaveBeenCalledTimes(1);
472471
});
473472

474-
it('should get locale from geolocation', async () => {
473+
it('should get locale from DDO', async () => {
475474
ipcinfoCookie.set.mockClear();
476475
ipcinfoCookie.get.mockImplementation(() => false);
477476

@@ -483,7 +482,7 @@ describe('LocaleAPI', () => {
483482
});
484483

485484
it('should get undefined locale on no cc', async () => {
486-
geolocation.mockImplementation(() => Promise.resolve(false));
485+
DDOAPI.getLocation.mockImplementation(() => Promise.resolve(false));
487486

488487
const locale = await LocaleAPI.getLocale();
489488
expect(locale).toBeUndefined();

packages/utilities/carbon.yml

+26-19
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
library:
33
id: ibmdotcom-utilities
44
name: IBM.com Utilities
5-
description: Apply JavaScript ES6 utility classes that adhere to IBM’s web standards.
5+
description:
6+
Apply JavaScript ES6 utility classes that adhere to IBM’s web standards.
67
externalDocsUrl: https://carbon-design-system.github.io/carbon-for-ibm-dotcom/utilities
78
noIndex: true
89
assets:
910
altlangs:
1011
name: altlangs
11-
description: Utility to grab all alternative languages on the page. this scrapes the page of all elements and returns as a readable object.
12+
description:
13+
Utility to grab all alternative languages on the page. this scrapes the
14+
page of all elements and returns as a readable object.
1215
type: function
1316
platform: web
1417
status: stable
@@ -30,7 +33,9 @@ assets:
3033
- utility
3134
decode-string:
3235
name: decodeString
33-
description: Utility function to parse and decode text content. Strings can become encoded for various reasons. This utility decodes those strings.
36+
description:
37+
Utility function to parse and decode text content. Strings can become
38+
encoded for various reasons. This utility decodes those strings.
3439
type: function
3540
platform: web
3641
status: stable
@@ -63,7 +68,9 @@ assets:
6368
- utility
6469
ipc-info-cookie:
6570
name: ipcinfoCookie
66-
description: Utility to set and get the ipcInfo cookie needed to determine country and language code.
71+
description:
72+
Utility to set and get the ipcInfo cookie needed to determine country and
73+
language code.
6774
type: function
6875
platform: web
6976
status: stable
@@ -74,7 +81,11 @@ assets:
7481
- utility
7582
format-video-caption:
7683
name: formatVideoCaption
77-
description: The default g11n formatter for video caption, combining video name and video duration. Components using this function should have a mechanism to allow translators to replace it with one accomodating the preferences of specific locale.
84+
description:
85+
The default g11n formatter for video caption, combining video name and
86+
video duration. Components using this function should have a mechanism to
87+
allow translators to replace it with one accomodating the preferences of
88+
specific locale.
7889
type: function
7990
platform: web
8091
status: stable
@@ -85,7 +96,12 @@ assets:
8596
- utility
8697
format-video-duration:
8798
name: formatVideoDuration
88-
description: The default g11n formatter for video duration. Components using this function should have a mechanism to allow translators to replace it with one accomodating the preferences of specific locale, or to replace it with general-purpose g11n formatting library. (e.g. moment, though it’s too big for us to make it a hard dependency)
99+
description:
100+
The default g11n formatter for video duration. Components using this
101+
function should have a mechanism to allow translators to replace it with
102+
one accomodating the preferences of specific locale, or to replace it with
103+
general-purpose g11n formatting library. (e.g. moment, though it’s too big
104+
for us to make it a hard dependency)
89105
type: function
90106
platform: web
91107
status: stable
@@ -94,20 +110,10 @@ assets:
94110
externalDocsUrl: https://carbon-design-system.github.io/carbon-for-ibm-dotcom/utilities/global.html#formatVideoDuration
95111
tags:
96112
- utility
97-
geolocation:
98-
name: geolocation
99-
description: Utility to retrieve user’s country code based on their IP address
100-
type: function
101-
platform: web
102-
status: stable
103-
framework: vanilla
104-
noIndex: true
105-
externalDocsUrl: https://carbon-design-system.github.io/carbon-for-ibm-dotcom/utilities/global.html#geolocation
106-
tags:
107-
- utility
108113
load-non-latin-plex:
109114
name: loadNonLatinPlex
110-
description: Utility to load in the corresponding non-Latin Plex font if necessary
115+
description:
116+
Utility to load in the corresponding non-Latin Plex font if necessary
111117
type: function
112118
platform: web
113119
status: stable
@@ -161,7 +167,8 @@ assets:
161167
- utility
162168
smooth-scroll:
163169
name: smoothScroll
164-
description: Utility handles smoothScroll on the anchor element after onClick
170+
description:
171+
Utility handles smoothScroll on the anchor element after onClick
165172
type: function
166173
platform: web
167174
status: stable

packages/utilities/src/utilities/geolocation/__tests__/geolocation.test.js

-31
This file was deleted.

packages/utilities/src/utilities/geolocation/geolocation.js

-36
This file was deleted.

packages/utilities/src/utilities/geolocation/index.js

-8
This file was deleted.

packages/utilities/src/utilities/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export * from './deprecate';
1212
export * from './escaperegexp';
1313
export * from './featureflag';
1414
export * from './formatVideoCaption';
15-
export * from './geolocation';
1615
export * from './ipcinfoCookie';
1716
export * from './loadNonLatinPlex';
1817
export * from './markdownToHtml';

packages/web-components/.env.example

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ SEARCH_TYPEAHEAD_API=<host for ibm.com search, e.g. https://www-api.ibm.com>
1313
MARKETING_SEARCH_VERSION=<api version for ibm.com marketing search, e.g. v3>
1414
MARKETING_SEARCH_HOST=<host for ibm.com marketing search, e.g. https://www.ibm.com>
1515

16-
# Geolocation
17-
GEO_API=<endpoint for getting user country by geolocation>
18-
1916
# VideoPlayer
2017
KALTURA_PARTNER_ID=<Kaltura partner id, e.g. 243342>
2118
KALTURA_UICONF_ID=<Kaltura uiconf id, e.g. 12905712>

0 commit comments

Comments
 (0)