@firebase/analytics 0.10.5 → 0.10.6-20240716220314

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,5 +1,7 @@
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';
3
5
  import { ErrorFactory, calculateBackoffMillis, FirebaseError, isIndexedDBAvailable, validateIndexedDBOpenable, isBrowserExtension, areCookiesEnabled, getModularInstance, deepEqual } from '@firebase/util';
4
6
  import { Component } from '@firebase/component';
5
7
  import '@firebase/installations';
@@ -65,65 +67,6 @@ const logger = new Logger('@firebase/analytics');
65
67
  * See the License for the specific language governing permissions and
66
68
  * limitations under the License.
67
69
  */
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-intialized 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
- }
127
70
  /**
128
71
  * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
129
72
  * have either resolved or rejected.
@@ -133,37 +76,21 @@ function createGtagTrustedTypesScriptURL(url) {
133
76
  function promiseAllSettled(promises) {
134
77
  return Promise.all(promises.map(promise => promise.catch(e => e)));
135
78
  }
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
- }
152
79
  /**
153
80
  * Inserts gtag script tag into the page to asynchronously download gtag.
154
81
  * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
155
82
  */
156
83
  function insertScriptTag(dataLayerName, measurementId) {
157
- const trustedTypesPolicy = createTrustedTypesPolicy('firebase-js-sdk-policy', {
158
- createScriptURL: createGtagTrustedTypesScriptURL
159
- });
160
84
  const script = document.createElement('script');
161
85
  // We are not providing an analyticsId in the URL because it would trigger a `page_view`
162
86
  // without fid. We will initialize ga-id using gtag (config) command together with fid.
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;
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);
167
94
  script.async = true;
168
95
  document.head.appendChild(script);
169
96
  }
@@ -386,6 +313,52 @@ function findGtagScriptOnPage(dataLayerName) {
386
313
  return null;
387
314
  }
388
315
 
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
+
389
362
  /**
390
363
  * @license
391
364
  * Copyright 2020 Google LLC
@@ -1229,7 +1202,7 @@ function setConsent(consentSettings) {
1229
1202
  }
1230
1203
 
1231
1204
  const name = "@firebase/analytics";
1232
- const version = "0.10.5";
1205
+ const version = "0.10.6-20240716220314";
1233
1206
 
1234
1207
  /**
1235
1208
  * The Firebase Analytics Web SDK.