Skip to content

Commit a03cab1

Browse files
Add app signup geo logic (#21609)
* update logic * update package * add test * update jest config
1 parent b52334a commit a03cab1

File tree

7 files changed

+87
-40
lines changed

7 files changed

+87
-40
lines changed

.babelrc

Lines changed: 0 additions & 19 deletions
This file was deleted.

assets/scripts/components/global-modals.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
import { DOMReady } from '../helpers/documentReady';
22
import { isMobile } from '../utils/isMobile';
3+
import { getGeoloc, getAppBaseUrl } from 'geo-locate';
34

45
const doOnLoad = () => {
56
const signupModal = document.getElementById('signupModal');
67

78
signupModal.addEventListener('show.bs.modal', () => {
8-
var regURL = 'https://app.datadoghq.com/signup_corp';
9-
var mobileURL = 'https://app.datadoghq.com/signup_corp?mobile=true';
10-
var lang_param = '';
11-
var lang = '';
12-
13-
if (document.documentElement.lang) {
14-
lang = document.documentElement.lang;
15-
} else {
16-
lang = ddc.lang;
17-
}
9+
10+
getGeoloc().then((loc) => {
11+
const baseUrl = `https://${getAppBaseUrl(loc.appRegion)}/signup_corp`;
12+
13+
var lang_param = '';
14+
var lang = '';
15+
16+
if (document.documentElement.lang) {
17+
lang = document.documentElement.lang;
18+
} else {
19+
lang = ddc.lang;
20+
}
1821

19-
if (lang === 'fr' || lang === 'ja') {
20-
lang_param = `?lang=${lang}`;
21-
} else {
22-
lang_param = '';
23-
}
22+
if (lang === 'fr' || lang === 'ja') {
23+
lang_param = `?lang=${lang}`;
24+
} else {
25+
lang_param = '';
26+
}
2427

25-
// Trigger conditional URL
26-
if (isMobile()) {
27-
document.querySelector('#signUpIframe').setAttribute('src', mobileURL);
28-
} else {
29-
document.querySelector('#signUpIframe').setAttribute('src', regURL + lang_param);
30-
}
28+
// Trigger conditional URL
29+
if (isMobile()) {
30+
document.querySelector('#signUpIframe').setAttribute('src', `${baseUrl}?mobile=true`);
31+
} else {
32+
document.querySelector('#signUpIframe').setAttribute('src', baseUrl + lang_param);
33+
}
34+
});
3135
});
3236

3337
signupModal.addEventListener('hide.bs.modal', () => {

assets/scripts/tests/geo.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import { getGeoloc } from 'geo-locate';
5+
6+
describe('getGeoLoc runs as expected', () => {
7+
8+
beforeEach(() => {
9+
10+
global.fetch = jest.fn(() =>
11+
Promise.resolve({
12+
json: () => Promise.resolve( {"country": "US", "is_eu": false, "app_region": "us1"} ),
13+
})
14+
);
15+
16+
// set up window, localStorage mock
17+
window.document.url = 'localhost';
18+
window.Storage = true;
19+
window.localStorage.__proto__.getItem = jest.fn();
20+
window.localStorage.__proto__.setItem = jest.fn();
21+
});
22+
23+
it('Should default US as the country and us1 as the region', () => {
24+
// run geo function
25+
getGeoloc().then((loc) => {
26+
expect(loc.country).toBe('US');
27+
expect(loc.appRegion).toBe('us1');
28+
29+
// make sure setItem is called
30+
expect(localStorage.setItem).toHaveBeenCalled();
31+
});
32+
33+
// fetch was executed as well
34+
expect(fetch).toHaveBeenCalledWith(
35+
"https://corpsite-staging.datadoghq.com/locate"
36+
);
37+
});
38+
});

babel.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current'
8+
}
9+
}
10+
]
11+
]
12+
}

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
transformIgnorePatterns: ['<rootDir>/node_modules/(?!geo-locate)'],
3+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"bootstrap": "^5.2",
4949
"del": "4.1.1",
5050
"fancy-log": "^1.3.3",
51+
"geo-locate": "https://s3.amazonaws.com/origin-static-assets/corp-node-packages/master/geo-locate-v1.0.1.tgz",
5152
"hugo-bin": "0.108.0",
5253
"instantsearch.js": "^4.53.0",
5354
"js-cookie": "^2.2.1",

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4327,6 +4327,7 @@ __metadata:
43274327
eslint-plugin-react-hooks: ^2.5.0
43284328
eslint-plugin-standard: ^4.0.0
43294329
fancy-log: ^1.3.3
4330+
geo-locate: "https://s3.amazonaws.com/origin-static-assets/corp-node-packages/master/geo-locate-v1.0.1.tgz"
43304331
hugo-bin: 0.108.0
43314332
instantsearch.js: ^4.53.0
43324333
jest: ^25.3.0
@@ -5562,6 +5563,13 @@ __metadata:
55625563
languageName: node
55635564
linkType: hard
55645565

5566+
"geo-locate@https://s3.amazonaws.com/origin-static-assets/corp-node-packages/master/geo-locate-v1.0.1.tgz":
5567+
version: 1.0.1
5568+
resolution: "geo-locate@https://s3.amazonaws.com/origin-static-assets/corp-node-packages/master/geo-locate-v1.0.1.tgz"
5569+
checksum: 85c4c20ded085cdc515c72e77a291d2f456402e76e6acb58b373ee8be1bf7c4430e780881c6157a1687299d29e10fcfd95f6b3972cb934a9f42c3be7e40af40e
5570+
languageName: node
5571+
linkType: hard
5572+
55655573
"get-caller-file@npm:^2.0.1":
55665574
version: 2.0.5
55675575
resolution: "get-caller-file@npm:2.0.5"

0 commit comments

Comments
 (0)