@firebase/analytics 0.10.6 → 0.10.7-canary.05c34c91e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,5 @@
1
1
  import { _getProvider, getApp, _registerComponent, registerVersion } from '@firebase/app';
2
2
  import { Logger } from '@firebase/logger';
3
- import { trustedResourceUrl } from 'safevalues';
4
- import { safeScriptEl } from 'safevalues/dom';
5
3
  import { ErrorFactory, calculateBackoffMillis, FirebaseError, isIndexedDBAvailable, validateIndexedDBOpenable, isBrowserExtension, areCookiesEnabled, getModularInstance, deepEqual } from '@firebase/util';
6
4
  import { Component } from '@firebase/component';
7
5
  import '@firebase/installations';
@@ -67,6 +65,65 @@ const logger = new Logger('@firebase/analytics');
67
65
  * See the License for the specific language governing permissions and
68
66
  * limitations under the License.
69
67
  */
68
+ const ERRORS = {
69
+ ["already-exists" /* AnalyticsError.ALREADY_EXISTS */]: 'A Firebase Analytics instance with the appId {$id} ' +
70
+ ' already exists. ' +
71
+ 'Only one Firebase Analytics instance can be created for each appId.',
72
+ ["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */]: 'initializeAnalytics() cannot be called again with different options than those ' +
73
+ 'it was initially called with. It can be called again with the same options to ' +
74
+ 'return the existing instance, or getAnalytics() can be used ' +
75
+ 'to get a reference to the already-initialized instance.',
76
+ ["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */]: 'Firebase Analytics has already been initialized.' +
77
+ 'settings() must be called before initializing any Analytics instance' +
78
+ 'or it will have no effect.',
79
+ ["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */]: 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
80
+ ["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */]: 'Firebase Analytics is not supported in this environment. ' +
81
+ 'Wrap initialization of analytics in analytics.isSupported() ' +
82
+ 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
83
+ ["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */]: 'IndexedDB unavailable or restricted in this environment. ' +
84
+ 'Wrap initialization of analytics in analytics.isSupported() ' +
85
+ 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
86
+ ["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */]: 'The config fetch request timed out while in an exponential backoff state.' +
87
+ ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
88
+ ["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */]: 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
89
+ ["no-api-key" /* AnalyticsError.NO_API_KEY */]: 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
90
+ 'contain a valid API key.',
91
+ ["no-app-id" /* AnalyticsError.NO_APP_ID */]: 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
92
+ 'contain a valid app ID.',
93
+ ["no-client-id" /* AnalyticsError.NO_CLIENT_ID */]: 'The "client_id" field is empty.',
94
+ ["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */]: 'Trusted Types detected an invalid gtag resource: {$gtagURL}.'
95
+ };
96
+ const ERROR_FACTORY = new ErrorFactory('analytics', 'Analytics', ERRORS);
97
+
98
+ /**
99
+ * @license
100
+ * Copyright 2019 Google LLC
101
+ *
102
+ * Licensed under the Apache License, Version 2.0 (the "License");
103
+ * you may not use this file except in compliance with the License.
104
+ * You may obtain a copy of the License at
105
+ *
106
+ * http://www.apache.org/licenses/LICENSE-2.0
107
+ *
108
+ * Unless required by applicable law or agreed to in writing, software
109
+ * distributed under the License is distributed on an "AS IS" BASIS,
110
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
111
+ * See the License for the specific language governing permissions and
112
+ * limitations under the License.
113
+ */
114
+ /**
115
+ * Verifies and creates a TrustedScriptURL.
116
+ */
117
+ function createGtagTrustedTypesScriptURL(url) {
118
+ if (!url.startsWith(GTAG_URL)) {
119
+ const err = ERROR_FACTORY.create("invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */, {
120
+ gtagURL: url
121
+ });
122
+ logger.warn(err.message);
123
+ return '';
124
+ }
125
+ return url;
126
+ }
70
127
  /**
71
128
  * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
72
129
  * have either resolved or rejected.
@@ -76,21 +133,37 @@ const logger = new Logger('@firebase/analytics');
76
133
  function promiseAllSettled(promises) {
77
134
  return Promise.all(promises.map(promise => promise.catch(e => e)));
78
135
  }
136
+ /**
137
+ * Creates a TrustedTypePolicy object that implements the rules passed as policyOptions.
138
+ *
139
+ * @param policyName A string containing the name of the policy
140
+ * @param policyOptions Object containing implementations of instance methods for TrustedTypesPolicy, see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy#instance_methods
141
+ * | the TrustedTypePolicy reference documentation}.
142
+ */
143
+ function createTrustedTypesPolicy(policyName, policyOptions) {
144
+ // Create a TrustedTypes policy that we can use for updating src
145
+ // properties
146
+ let trustedTypesPolicy;
147
+ if (window.trustedTypes) {
148
+ trustedTypesPolicy = window.trustedTypes.createPolicy(policyName, policyOptions);
149
+ }
150
+ return trustedTypesPolicy;
151
+ }
79
152
  /**
80
153
  * Inserts gtag script tag into the page to asynchronously download gtag.
81
154
  * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
82
155
  */
83
156
  function insertScriptTag(dataLayerName, measurementId) {
157
+ const trustedTypesPolicy = createTrustedTypesPolicy('firebase-js-sdk-policy', {
158
+ createScriptURL: createGtagTrustedTypesScriptURL
159
+ });
84
160
  const script = document.createElement('script');
85
161
  // We are not providing an analyticsId in the URL because it would trigger a `page_view`
86
162
  // without fid. We will initialize ga-id using gtag (config) command together with fid.
87
- //
88
- // We also have to ensure the template string before the first expression constitutes a valid URL
89
- // start, as this is what the initial validation focuses on. If the template literal begins
90
- // directly with an expression (e.g. `${GTAG_SCRIPT_URL}`), the validation fails due to an
91
- // empty initial string.
92
- const url = trustedResourceUrl `https://www.googletagmanager.com/gtag/js?l=${dataLayerName}&id=${measurementId}`;
93
- safeScriptEl.setSrc(script, url);
163
+ const gtagScriptURL = `${GTAG_URL}?l=${dataLayerName}&id=${measurementId}`;
164
+ script.src = trustedTypesPolicy
165
+ ? trustedTypesPolicy === null || trustedTypesPolicy === void 0 ? void 0 : trustedTypesPolicy.createScriptURL(gtagScriptURL)
166
+ : gtagScriptURL;
94
167
  script.async = true;
95
168
  document.head.appendChild(script);
96
169
  }
@@ -187,6 +260,7 @@ async function gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPro
187
260
  // if not all entries in the 'send_to' field could be mapped to
188
261
  // a FID. In these cases, wait on all pending initialization promises.
189
262
  if (initializationPromisesToWaitFor.length === 0) {
263
+ /* eslint-disable-next-line @typescript-eslint/no-floating-promises */
190
264
  initializationPromisesToWaitFor = Object.values(initializationPromisesMap);
191
265
  }
192
266
  // Run core gtag function with args after all relevant initialization
@@ -210,7 +284,7 @@ async function gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPro
210
284
  */
211
285
  function wrapGtag(gtagCore,
212
286
  /**
213
- * Allows wrapped gtag calls to wait on whichever intialization promises are required,
287
+ * Allows wrapped gtag calls to wait on whichever initialization promises are required,
214
288
  * depending on the contents of the gtag params' `send_to` field, if any.
215
289
  */
216
290
  initializationPromisesMap,
@@ -313,52 +387,6 @@ function findGtagScriptOnPage(dataLayerName) {
313
387
  return null;
314
388
  }
315
389
 
316
- /**
317
- * @license
318
- * Copyright 2019 Google LLC
319
- *
320
- * Licensed under the Apache License, Version 2.0 (the "License");
321
- * you may not use this file except in compliance with the License.
322
- * You may obtain a copy of the License at
323
- *
324
- * http://www.apache.org/licenses/LICENSE-2.0
325
- *
326
- * Unless required by applicable law or agreed to in writing, software
327
- * distributed under the License is distributed on an "AS IS" BASIS,
328
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
329
- * See the License for the specific language governing permissions and
330
- * limitations under the License.
331
- */
332
- const ERRORS = {
333
- ["already-exists" /* AnalyticsError.ALREADY_EXISTS */]: 'A Firebase Analytics instance with the appId {$id} ' +
334
- ' already exists. ' +
335
- 'Only one Firebase Analytics instance can be created for each appId.',
336
- ["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */]: 'initializeAnalytics() cannot be called again with different options than those ' +
337
- 'it was initially called with. It can be called again with the same options to ' +
338
- 'return the existing instance, or getAnalytics() can be used ' +
339
- 'to get a reference to the already-intialized instance.',
340
- ["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */]: 'Firebase Analytics has already been initialized.' +
341
- 'settings() must be called before initializing any Analytics instance' +
342
- 'or it will have no effect.',
343
- ["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */]: 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
344
- ["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */]: 'Firebase Analytics is not supported in this environment. ' +
345
- 'Wrap initialization of analytics in analytics.isSupported() ' +
346
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
347
- ["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */]: 'IndexedDB unavailable or restricted in this environment. ' +
348
- 'Wrap initialization of analytics in analytics.isSupported() ' +
349
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
350
- ["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */]: 'The config fetch request timed out while in an exponential backoff state.' +
351
- ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
352
- ["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */]: 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
353
- ["no-api-key" /* AnalyticsError.NO_API_KEY */]: 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
354
- 'contain a valid API key.',
355
- ["no-app-id" /* AnalyticsError.NO_APP_ID */]: 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
356
- 'contain a valid app ID.',
357
- ["no-client-id" /* AnalyticsError.NO_CLIENT_ID */]: 'The "client_id" field is empty.',
358
- ["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */]: 'Trusted Types detected an invalid gtag resource: {$gtagURL}.'
359
- };
360
- const ERROR_FACTORY = new ErrorFactory('analytics', 'Analytics', ERRORS);
361
-
362
390
  /**
363
391
  * @license
364
392
  * Copyright 2020 Google LLC
@@ -1202,7 +1230,7 @@ function setConsent(consentSettings) {
1202
1230
  }
1203
1231
 
1204
1232
  const name = "@firebase/analytics";
1205
- const version = "0.10.6";
1233
+ const version = "0.10.7-canary.05c34c91e";
1206
1234
 
1207
1235
  /**
1208
1236
  * The Firebase Analytics Web SDK.