@@ -19,6 +19,7 @@ import getHost from './get-host';
1919import baseCookie from './base-cookie' ;
2020import { AmplitudeServerZone , getEventLogApi } from './server-zone' ;
2121import ConfigManager from './config-manager' ;
22+ import GlobalScope from './global-scope' ;
2223
2324/**
2425 * AmplitudeClient SDK API - instance constructor.
@@ -28,7 +29,7 @@ import ConfigManager from './config-manager';
2829 * @example var amplitudeClient = new AmplitudeClient();
2930 */
3031var AmplitudeClient = function AmplitudeClient ( instanceName ) {
31- if ( ! isBrowserEnv ( ) ) {
32+ if ( ! isBrowserEnv ( ) && ! utils . isWebWorkerEnvironment ( ) ) {
3233 utils . log . warn (
3334 'amplitude-js will not work in a non-browser environment. If you are planning to add Amplitude to a node environment, please use @amplitude/node' ,
3435 ) ;
@@ -80,7 +81,11 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
8081
8182 try {
8283 _parseConfig ( this . options , opt_config ) ;
83- if ( isBrowserEnv ( ) && window . Prototype !== undefined && Array . prototype . toJSON ) {
84+ if (
85+ ( isBrowserEnv ( ) || utils . isWebWorkerEnvironment ( ) ) &&
86+ GlobalScope . Prototype !== undefined &&
87+ Array . prototype . toJSON
88+ ) {
8489 prototypeJsFix ( ) ;
8590 utils . log . warn (
8691 'Prototype.js injected Array.prototype.toJSON. Deleting Array.prototype.toJSON to prevent double-stringify' ,
@@ -243,7 +248,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
243248 // Monitoring just page exits because that is the most requested feature for now
244249 // "If you're specifically trying to detect page unload events, the pagehide event is the best option."
245250 // https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
246- window . addEventListener (
251+ GlobalScope . addEventListener (
247252 'pagehide' ,
248253 ( ) => {
249254 handleVisibilityChange ( ) ;
@@ -254,7 +259,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
254259 }
255260 } catch ( err ) {
256261 utils . log . error ( err ) ;
257- if ( type ( opt_config . onError ) === 'function' ) {
262+ if ( opt_config && type ( opt_config . onError ) === 'function' ) {
258263 opt_config . onError ( err ) ;
259264 }
260265 }
@@ -272,7 +277,7 @@ AmplitudeClient.prototype.deleteLowerLevelDomainCookies = function () {
272277 const cookieHost =
273278 this . options . domain && this . options . domain [ 0 ] === '.' ? this . options . domain . slice ( 1 ) : this . options . domain ;
274279
275- if ( ! cookieHost ) {
280+ if ( ! cookieHost || ! utils . isWebWorkerEnvironment ( ) ) {
276281 return ;
277282 }
278283
@@ -751,14 +756,14 @@ var _sendParamsReferrerUserProperties = function _sendParamsReferrerUserProperti
751756 * @private
752757 */
753758AmplitudeClient . prototype . _getReferrer = function _getReferrer ( ) {
754- return document . referrer ;
759+ return typeof document !== 'undefined' ? document . referrer : '' ;
755760} ;
756761
757762/**
758763 * @private
759764 */
760765AmplitudeClient . prototype . _getUrlParams = function _getUrlParams ( ) {
761- return location . search ;
766+ return GlobalScope . location . search ;
762767} ;
763768
764769/**
@@ -1780,7 +1785,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
17801785 }
17811786 this . _sending = true ;
17821787 }
1783- var protocol = this . options . forceHttps ? 'https' : 'https:' === window . location . protocol ? 'https' : 'http' ;
1788+ var protocol = this . options . forceHttps ? 'https' : 'https:' === GlobalScope . location . protocol ? 'https' : 'http' ;
17841789 var url = protocol + '://' + this . options . apiEndpoint ;
17851790
17861791 // fetch events to send
0 commit comments