Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add guard for navigator for use in envs that do no support navig… #542

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@babel/runtime": "^7.3.4",
"blueimp-md5": "^2.10.0",
"query-string": "5",
"@amplitude/analytics-connector": "1.4.3"
"@amplitude/analytics-connector": "1.4.4"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
"devDependencies": {
"@amplitude/eslint-plugin-amplitude": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
this._instanceName = utils.isEmptyString(instanceName) ? Constants.DEFAULT_INSTANCE : instanceName.toLowerCase();
this._unsentEvents = [];
this._unsentIdentifys = [];
this._ua = new UAParser(navigator.userAgent).getResult();
this.options = { ...DEFAULT_OPTIONS, trackingOptions: { ...DEFAULT_OPTIONS.trackingOptions } };
this.cookieStorage = new cookieStorage().getStorage();
this._q = []; // queue for proxied functions before script load
Expand All @@ -61,7 +60,8 @@ var AmplitudeClient = function AmplitudeClient(instanceName) {
// used to integrate with experiment SDK (client-side exposure tracking & real-time user properties)
this._connector = null;

this._userAgent = (navigator && navigator.userAgent) || null;
this._userAgent = (typeof navigator !== 'undefined' && navigator && navigator.userAgent) || null;
this._ua = new UAParser(this._userAgent).getResult();
};

AmplitudeClient.prototype.Identify = Identify;
Expand Down Expand Up @@ -249,7 +249,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
}

const onExitPage = this.options.onExitPage;
if (type(onExitPage) === 'function') {
if (type(onExitPage) === 'function' && GlobalScope.addEventListener) {
if (!this.pageHandlersAdded) {
this.pageHandlersAdded = true;

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

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

if (success) {
Expand Down
7 changes: 4 additions & 3 deletions src/get-host.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import GlobalScope from './global-scope';

const getHost = (url) => {
const defaultHostname = GlobalScope.location ? GlobalScope.location.hostname : '';
if (url) {
if (typeof document !== 'undefined') {
const a = document.createElement('a');
a.href = url;
return a.hostname || GlobalScope.location.hostname;
return a.hostname || defaultHostname;
}
if (typeof URL === 'function') {
const u = new URL(url);
return u.hostname || GlobalScope.location.hostname;
return u.hostname || defaultHostname;
}
}
return GlobalScope.location.hostname;
return defaultHostname;
};

export default getHost;
4 changes: 2 additions & 2 deletions src/metadata-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MetadataStorage {
this.expirationDays = expirationDays;

this.cookieDomain = '';

const writableTopDomain = topDomain(getLocation().href);
const loc = getLocation() ? getLocation().href : undefined;
const writableTopDomain = topDomain(loc);
this.cookieDomain = domain || (writableTopDomain ? '.' + writableTopDomain : null);

if (storageOptionExists[storage]) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const validateTransport = function validateTransport(transport) {
return false;
}

if (transport !== constants.TRANSPORT_HTTP && !navigator.sendBeacon) {
if (transport !== constants.TRANSPORT_HTTP && typeof navigator !== 'undefined' && !navigator.sendBeacon) {
log.error(`browser does not support sendBeacon, so transport must be HTTP`);
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


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

Expand Down