Skip to content

Commit c3b31ad

Browse files
fix: add guard for navigator for use in envs that do no support navigator (#542)
1 parent a453dc3 commit c3b31ad

6 files changed

+16
-15
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@babel/runtime": "^7.3.4",
2121
"blueimp-md5": "^2.10.0",
2222
"query-string": "5",
23-
"@amplitude/analytics-connector": "1.4.3"
23+
"@amplitude/analytics-connector": "1.4.4"
2424
},
2525
"devDependencies": {
2626
"@amplitude/eslint-plugin-amplitude": "^1.0.1",

src/amplitude-client.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
3939
this._instanceName = utils.isEmptyString(instanceName) ? Constants.DEFAULT_INSTANCE : instanceName.toLowerCase();
4040
this._unsentEvents = [];
4141
this._unsentIdentifys = [];
42-
this._ua = new UAParser(navigator.userAgent).getResult();
4342
this.options = { ...DEFAULT_OPTIONS, trackingOptions: { ...DEFAULT_OPTIONS.trackingOptions } };
4443
this.cookieStorage = new cookieStorage().getStorage();
4544
this._q = []; // queue for proxied functions before script load
@@ -61,7 +60,8 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
6160
// used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
6261
this._connector = null;
6362

64-
this._userAgent = (navigator && navigator.userAgent) || null;
63+
this._userAgent = (typeof navigator !== 'undefined' && navigator && navigator.userAgent) || null;
64+
this._ua = new UAParser(this._userAgent).getResult();
6565
};
6666

6767
AmplitudeClient.prototype.Identify = Identify;
@@ -249,7 +249,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
249249
}
250250

251251
const onExitPage = this.options.onExitPage;
252-
if (type(onExitPage) === 'function') {
252+
if (type(onExitPage) === 'function' && GlobalScope.addEventListener) {
253253
if (!this.pageHandlersAdded) {
254254
this.pageHandlersAdded = true;
255255

@@ -1865,7 +1865,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
18651865
checksum: md5(Constants.API_VERSION + this.options.apiKey + events + uploadTime),
18661866
};
18671867

1868-
if (this.options.transport === Constants.TRANSPORT_BEACON) {
1868+
if (this.options.transport === Constants.TRANSPORT_BEACON && typeof navigator !== 'undefined') {
18691869
const success = navigator.sendBeacon(url, new URLSearchParams(data));
18701870

18711871
if (success) {

src/get-host.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import GlobalScope from './global-scope';
22

33
const getHost = (url) => {
4+
const defaultHostname = GlobalScope.location ? GlobalScope.location.hostname : '';
45
if (url) {
56
if (typeof document !== 'undefined') {
67
const a = document.createElement('a');
78
a.href = url;
8-
return a.hostname || GlobalScope.location.hostname;
9+
return a.hostname || defaultHostname;
910
}
1011
if (typeof URL === 'function') {
1112
const u = new URL(url);
12-
return u.hostname || GlobalScope.location.hostname;
13+
return u.hostname || defaultHostname;
1314
}
1415
}
15-
return GlobalScope.location.hostname;
16+
return defaultHostname;
1617
};
1718

1819
export default getHost;

src/metadata-storage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class MetadataStorage {
3535
this.expirationDays = expirationDays;
3636

3737
this.cookieDomain = '';
38-
39-
const writableTopDomain = topDomain(getLocation().href);
38+
const loc = getLocation() ? getLocation().href : undefined;
39+
const writableTopDomain = topDomain(loc);
4040
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);
4141

4242
if (storageOptionExists[storage]) {

src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const validateTransport = function validateTransport(transport) {
119119
return false;
120120
}
121121

122-
if (transport !== constants.TRANSPORT_HTTP && !navigator.sendBeacon) {
122+
if (transport !== constants.TRANSPORT_HTTP && typeof navigator !== 'undefined' && !navigator.sendBeacon) {
123123
log.error(`browser does not support sendBeacon, so transport must be HTTP`);
124124
return false;
125125
}

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# yarn lockfile v1
33

44

5-
"@amplitude/[email protected].3":
6-
version "1.4.3"
7-
resolved "https://registry.yarnpkg.com/@amplitude/analytics-connector/-/analytics-connector-1.4.3.tgz#cb9098cb8adfbd39b2c71ad71a7c758504a745a7"
8-
integrity sha512-Ghu1UJn55Rn9eglF+ED7yOGXaeX3KY2qkQi9W9yqC02ItPvKfrybeVndweI1XtsiW0LvRpdA3uQEjuZEGunyLw==
5+
"@amplitude/[email protected].4":
6+
version "1.4.4"
7+
resolved "https://registry.yarnpkg.com/@amplitude/analytics-connector/-/analytics-connector-1.4.4.tgz#3bed72b0b3190a4426a529473ced962892706116"
8+
integrity sha512-6JcE1nxrprJt6pHqqDQb7FXRqJmFHG7KJPe0jNZaAvfll4mWKVqZu8W9IV3XiN1P+xgHIV1NN+i3PLOAZWEhXg==
99
dependencies:
1010
"@amplitude/ua-parser-js" "0.7.31"
1111

0 commit comments

Comments
 (0)