@firebase/analytics 0.9.4 → 0.9.5-canary.a8d6499b1

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.
@@ -65,6 +65,64 @@ const logger = new Logger('@firebase/analytics');
65
65
  * See the License for the specific language governing permissions and
66
66
  * limitations under the License.
67
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-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
+ ["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */]: 'Trusted Types detected an invalid gtag resource: {$gtagURL}.'
94
+ };
95
+ const ERROR_FACTORY = new ErrorFactory('analytics', 'Analytics', ERRORS);
96
+
97
+ /**
98
+ * @license
99
+ * Copyright 2019 Google LLC
100
+ *
101
+ * Licensed under the Apache License, Version 2.0 (the "License");
102
+ * you may not use this file except in compliance with the License.
103
+ * You may obtain a copy of the License at
104
+ *
105
+ * http://www.apache.org/licenses/LICENSE-2.0
106
+ *
107
+ * Unless required by applicable law or agreed to in writing, software
108
+ * distributed under the License is distributed on an "AS IS" BASIS,
109
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
110
+ * See the License for the specific language governing permissions and
111
+ * limitations under the License.
112
+ */
113
+ /**
114
+ * Verifies and creates a TrustedScriptURL.
115
+ */
116
+ function createGtagTrustedTypesScriptURL(url) {
117
+ if (!url.startsWith(GTAG_URL)) {
118
+ const err = ERROR_FACTORY.create("invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */, {
119
+ gtagURL: url
120
+ });
121
+ logger.warn(err.message);
122
+ return '';
123
+ }
124
+ return url;
125
+ }
68
126
  /**
69
127
  * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
70
128
  * have either resolved or rejected.
@@ -74,15 +132,37 @@ const logger = new Logger('@firebase/analytics');
74
132
  function promiseAllSettled(promises) {
75
133
  return Promise.all(promises.map(promise => promise.catch(e => e)));
76
134
  }
135
+ /**
136
+ * Creates a TrustedTypePolicy object that implements the rules passed as policyOptions.
137
+ *
138
+ * @param policyName A string containing the name of the policy
139
+ * @param policyOptions Object containing implementations of instance methods for TrustedTypesPolicy, see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy#instance_methods
140
+ * | the TrustedTypePolicy reference documentation}.
141
+ */
142
+ function createTrustedTypesPolicy(policyName, policyOptions) {
143
+ // Create a TrustedTypes policy that we can use for updating src
144
+ // properties
145
+ let trustedTypesPolicy;
146
+ if (window.trustedTypes) {
147
+ trustedTypesPolicy = window.trustedTypes.createPolicy(policyName, policyOptions);
148
+ }
149
+ return trustedTypesPolicy;
150
+ }
77
151
  /**
78
152
  * Inserts gtag script tag into the page to asynchronously download gtag.
79
153
  * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
80
154
  */
81
155
  function insertScriptTag(dataLayerName, measurementId) {
156
+ const trustedTypesPolicy = createTrustedTypesPolicy('firebase-js-sdk-policy', {
157
+ createScriptURL: createGtagTrustedTypesScriptURL
158
+ });
82
159
  const script = document.createElement('script');
83
160
  // We are not providing an analyticsId in the URL because it would trigger a `page_view`
84
161
  // without fid. We will initialize ga-id using gtag (config) command together with fid.
85
- script.src = `${GTAG_URL}?l=${dataLayerName}&id=${measurementId}`;
162
+ const gtagScriptURL = `${GTAG_URL}?l=${dataLayerName}&id=${measurementId}`;
163
+ script.src = trustedTypesPolicy
164
+ ? trustedTypesPolicy === null || trustedTypesPolicy === void 0 ? void 0 : trustedTypesPolicy.createScriptURL(gtagScriptURL)
165
+ : gtagScriptURL;
86
166
  script.async = true;
87
167
  document.head.appendChild(script);
88
168
  }
@@ -294,50 +374,6 @@ function findGtagScriptOnPage(dataLayerName) {
294
374
  return null;
295
375
  }
296
376
 
297
- /**
298
- * @license
299
- * Copyright 2019 Google LLC
300
- *
301
- * Licensed under the Apache License, Version 2.0 (the "License");
302
- * you may not use this file except in compliance with the License.
303
- * You may obtain a copy of the License at
304
- *
305
- * http://www.apache.org/licenses/LICENSE-2.0
306
- *
307
- * Unless required by applicable law or agreed to in writing, software
308
- * distributed under the License is distributed on an "AS IS" BASIS,
309
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
310
- * See the License for the specific language governing permissions and
311
- * limitations under the License.
312
- */
313
- const ERRORS = {
314
- ["already-exists" /* AnalyticsError.ALREADY_EXISTS */]: 'A Firebase Analytics instance with the appId {$id} ' +
315
- ' already exists. ' +
316
- 'Only one Firebase Analytics instance can be created for each appId.',
317
- ["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */]: 'initializeAnalytics() cannot be called again with different options than those ' +
318
- 'it was initially called with. It can be called again with the same options to ' +
319
- 'return the existing instance, or getAnalytics() can be used ' +
320
- 'to get a reference to the already-intialized instance.',
321
- ["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */]: 'Firebase Analytics has already been initialized.' +
322
- 'settings() must be called before initializing any Analytics instance' +
323
- 'or it will have no effect.',
324
- ["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */]: 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
325
- ["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */]: 'Firebase Analytics is not supported in this environment. ' +
326
- 'Wrap initialization of analytics in analytics.isSupported() ' +
327
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
328
- ["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */]: 'IndexedDB unavailable or restricted in this environment. ' +
329
- 'Wrap initialization of analytics in analytics.isSupported() ' +
330
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
331
- ["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */]: 'The config fetch request timed out while in an exponential backoff state.' +
332
- ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
333
- ["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */]: 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
334
- ["no-api-key" /* AnalyticsError.NO_API_KEY */]: 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
335
- 'contain a valid API key.',
336
- ["no-app-id" /* AnalyticsError.NO_APP_ID */]: 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
337
- 'contain a valid app ID.'
338
- };
339
- const ERROR_FACTORY = new ErrorFactory('analytics', 'Analytics', ERRORS);
340
-
341
377
  /**
342
378
  * @license
343
379
  * Copyright 2020 Google LLC
@@ -1152,7 +1188,7 @@ function setConsent(consentSettings) {
1152
1188
  }
1153
1189
 
1154
1190
  const name = "@firebase/analytics";
1155
- const version = "0.9.4";
1191
+ const version = "0.9.5-canary.a8d6499b1";
1156
1192
 
1157
1193
  /**
1158
1194
  * Firebase Analytics