@@ -19,6 +19,7 @@ import getHost from './get-host';
19
19
import baseCookie from './base-cookie' ;
20
20
import { AmplitudeServerZone , getEventLogApi } from './server-zone' ;
21
21
import ConfigManager from './config-manager' ;
22
+ import GlobalScope from './global-scope' ;
22
23
23
24
/**
24
25
* AmplitudeClient SDK API - instance constructor.
@@ -28,7 +29,7 @@ import ConfigManager from './config-manager';
28
29
* @example var amplitudeClient = new AmplitudeClient();
29
30
*/
30
31
var AmplitudeClient = function AmplitudeClient ( instanceName ) {
31
- if ( ! isBrowserEnv ( ) ) {
32
+ if ( ! isBrowserEnv ( ) && ! utils . isWebWorkerEnvironment ( ) ) {
32
33
utils . log . warn (
33
34
'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' ,
34
35
) ;
@@ -80,7 +81,11 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
80
81
81
82
try {
82
83
_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
+ ) {
84
89
prototypeJsFix ( ) ;
85
90
utils . log . warn (
86
91
'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
243
248
// Monitoring just page exits because that is the most requested feature for now
244
249
// "If you're specifically trying to detect page unload events, the pagehide event is the best option."
245
250
// https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
246
- window . addEventListener (
251
+ GlobalScope . addEventListener (
247
252
'pagehide' ,
248
253
( ) => {
249
254
handleVisibilityChange ( ) ;
@@ -254,7 +259,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
254
259
}
255
260
} catch ( err ) {
256
261
utils . log . error ( err ) ;
257
- if ( type ( opt_config . onError ) === 'function' ) {
262
+ if ( opt_config && type ( opt_config . onError ) === 'function' ) {
258
263
opt_config . onError ( err ) ;
259
264
}
260
265
}
@@ -272,7 +277,7 @@ AmplitudeClient.prototype.deleteLowerLevelDomainCookies = function () {
272
277
const cookieHost =
273
278
this . options . domain && this . options . domain [ 0 ] === '.' ? this . options . domain . slice ( 1 ) : this . options . domain ;
274
279
275
- if ( ! cookieHost ) {
280
+ if ( ! cookieHost || ! utils . isWebWorkerEnvironment ( ) ) {
276
281
return ;
277
282
}
278
283
@@ -751,14 +756,14 @@ var _sendParamsReferrerUserProperties = function _sendParamsReferrerUserProperti
751
756
* @private
752
757
*/
753
758
AmplitudeClient . prototype . _getReferrer = function _getReferrer ( ) {
754
- return document . referrer ;
759
+ return typeof document !== 'undefined' ? document . referrer : '' ;
755
760
} ;
756
761
757
762
/**
758
763
* @private
759
764
*/
760
765
AmplitudeClient . prototype . _getUrlParams = function _getUrlParams ( ) {
761
- return location . search ;
766
+ return GlobalScope . location . search ;
762
767
} ;
763
768
764
769
/**
@@ -1779,7 +1784,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() {
1779
1784
}
1780
1785
this . _sending = true ;
1781
1786
}
1782
- var protocol = this . options . forceHttps ? 'https' : 'https:' === window . location . protocol ? 'https' : 'http' ;
1787
+ var protocol = this . options . forceHttps ? 'https' : 'https:' === GlobalScope . location . protocol ? 'https' : 'http' ;
1783
1788
var url = protocol + '://' + this . options . apiEndpoint ;
1784
1789
1785
1790
// fetch events to send
0 commit comments