@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.
package/dist/index.cjs.js CHANGED
@@ -70,6 +70,65 @@ var logger = new logger$1.Logger('@firebase/analytics');
70
70
  * See the License for the specific language governing permissions and
71
71
  * limitations under the License.
72
72
  */
73
+ var _a;
74
+ var ERRORS = (_a = {},
75
+ _a["already-exists" /* AnalyticsError.ALREADY_EXISTS */] = 'A Firebase Analytics instance with the appId {$id} ' +
76
+ ' already exists. ' +
77
+ 'Only one Firebase Analytics instance can be created for each appId.',
78
+ _a["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */] = 'initializeAnalytics() cannot be called again with different options than those ' +
79
+ 'it was initially called with. It can be called again with the same options to ' +
80
+ 'return the existing instance, or getAnalytics() can be used ' +
81
+ 'to get a reference to the already-intialized instance.',
82
+ _a["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */] = 'Firebase Analytics has already been initialized.' +
83
+ 'settings() must be called before initializing any Analytics instance' +
84
+ 'or it will have no effect.',
85
+ _a["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */] = 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
86
+ _a["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */] = 'Firebase Analytics is not supported in this environment. ' +
87
+ 'Wrap initialization of analytics in analytics.isSupported() ' +
88
+ 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
89
+ _a["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */] = 'IndexedDB unavailable or restricted in this environment. ' +
90
+ 'Wrap initialization of analytics in analytics.isSupported() ' +
91
+ 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
92
+ _a["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */] = 'The config fetch request timed out while in an exponential backoff state.' +
93
+ ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
94
+ _a["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */] = 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
95
+ _a["no-api-key" /* AnalyticsError.NO_API_KEY */] = 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
96
+ 'contain a valid API key.',
97
+ _a["no-app-id" /* AnalyticsError.NO_APP_ID */] = 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
98
+ 'contain a valid app ID.',
99
+ _a["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */] = 'Trusted Types detected an invalid gtag resource: {$gtagURL}.',
100
+ _a);
101
+ var ERROR_FACTORY = new util.ErrorFactory('analytics', 'Analytics', ERRORS);
102
+
103
+ /**
104
+ * @license
105
+ * Copyright 2019 Google LLC
106
+ *
107
+ * Licensed under the Apache License, Version 2.0 (the "License");
108
+ * you may not use this file except in compliance with the License.
109
+ * You may obtain a copy of the License at
110
+ *
111
+ * http://www.apache.org/licenses/LICENSE-2.0
112
+ *
113
+ * Unless required by applicable law or agreed to in writing, software
114
+ * distributed under the License is distributed on an "AS IS" BASIS,
115
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
116
+ * See the License for the specific language governing permissions and
117
+ * limitations under the License.
118
+ */
119
+ /**
120
+ * Verifies and creates a TrustedScriptURL.
121
+ */
122
+ function createGtagTrustedTypesScriptURL(url) {
123
+ if (!url.startsWith(GTAG_URL)) {
124
+ var err = ERROR_FACTORY.create("invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */, {
125
+ gtagURL: url
126
+ });
127
+ logger.warn(err.message);
128
+ return '';
129
+ }
130
+ return url;
131
+ }
73
132
  /**
74
133
  * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
75
134
  * have either resolved or rejected.
@@ -79,15 +138,37 @@ var logger = new logger$1.Logger('@firebase/analytics');
79
138
  function promiseAllSettled(promises) {
80
139
  return Promise.all(promises.map(function (promise) { return promise.catch(function (e) { return e; }); }));
81
140
  }
141
+ /**
142
+ * Creates a TrustedTypePolicy object that implements the rules passed as policyOptions.
143
+ *
144
+ * @param policyName A string containing the name of the policy
145
+ * @param policyOptions Object containing implementations of instance methods for TrustedTypesPolicy, see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy#instance_methods
146
+ * | the TrustedTypePolicy reference documentation}.
147
+ */
148
+ function createTrustedTypesPolicy(policyName, policyOptions) {
149
+ // Create a TrustedTypes policy that we can use for updating src
150
+ // properties
151
+ var trustedTypesPolicy;
152
+ if (window.trustedTypes) {
153
+ trustedTypesPolicy = window.trustedTypes.createPolicy(policyName, policyOptions);
154
+ }
155
+ return trustedTypesPolicy;
156
+ }
82
157
  /**
83
158
  * Inserts gtag script tag into the page to asynchronously download gtag.
84
159
  * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
85
160
  */
86
161
  function insertScriptTag(dataLayerName, measurementId) {
162
+ var trustedTypesPolicy = createTrustedTypesPolicy('firebase-js-sdk-policy', {
163
+ createScriptURL: createGtagTrustedTypesScriptURL
164
+ });
87
165
  var script = document.createElement('script');
88
166
  // We are not providing an analyticsId in the URL because it would trigger a `page_view`
89
167
  // without fid. We will initialize ga-id using gtag (config) command together with fid.
90
- script.src = "".concat(GTAG_URL, "?l=").concat(dataLayerName, "&id=").concat(measurementId);
168
+ var gtagScriptURL = "".concat(GTAG_URL, "?l=").concat(dataLayerName, "&id=").concat(measurementId);
169
+ script.src = trustedTypesPolicy
170
+ ? trustedTypesPolicy === null || trustedTypesPolicy === void 0 ? void 0 : trustedTypesPolicy.createScriptURL(gtagScriptURL)
171
+ : gtagScriptURL;
91
172
  script.async = true;
92
173
  document.head.appendChild(script);
93
174
  }
@@ -348,51 +429,6 @@ function findGtagScriptOnPage(dataLayerName) {
348
429
  return null;
349
430
  }
350
431
 
351
- /**
352
- * @license
353
- * Copyright 2019 Google LLC
354
- *
355
- * Licensed under the Apache License, Version 2.0 (the "License");
356
- * you may not use this file except in compliance with the License.
357
- * You may obtain a copy of the License at
358
- *
359
- * http://www.apache.org/licenses/LICENSE-2.0
360
- *
361
- * Unless required by applicable law or agreed to in writing, software
362
- * distributed under the License is distributed on an "AS IS" BASIS,
363
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
364
- * See the License for the specific language governing permissions and
365
- * limitations under the License.
366
- */
367
- var _a;
368
- var ERRORS = (_a = {},
369
- _a["already-exists" /* AnalyticsError.ALREADY_EXISTS */] = 'A Firebase Analytics instance with the appId {$id} ' +
370
- ' already exists. ' +
371
- 'Only one Firebase Analytics instance can be created for each appId.',
372
- _a["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */] = 'initializeAnalytics() cannot be called again with different options than those ' +
373
- 'it was initially called with. It can be called again with the same options to ' +
374
- 'return the existing instance, or getAnalytics() can be used ' +
375
- 'to get a reference to the already-intialized instance.',
376
- _a["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */] = 'Firebase Analytics has already been initialized.' +
377
- 'settings() must be called before initializing any Analytics instance' +
378
- 'or it will have no effect.',
379
- _a["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */] = 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
380
- _a["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */] = 'Firebase Analytics is not supported in this environment. ' +
381
- 'Wrap initialization of analytics in analytics.isSupported() ' +
382
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
383
- _a["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */] = 'IndexedDB unavailable or restricted in this environment. ' +
384
- 'Wrap initialization of analytics in analytics.isSupported() ' +
385
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
386
- _a["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */] = 'The config fetch request timed out while in an exponential backoff state.' +
387
- ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
388
- _a["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */] = 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
389
- _a["no-api-key" /* AnalyticsError.NO_API_KEY */] = 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
390
- 'contain a valid API key.',
391
- _a["no-app-id" /* AnalyticsError.NO_APP_ID */] = 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
392
- 'contain a valid app ID.',
393
- _a);
394
- var ERROR_FACTORY = new util.ErrorFactory('analytics', 'Analytics', ERRORS);
395
-
396
432
  /**
397
433
  * @license
398
434
  * Copyright 2020 Google LLC
@@ -1326,7 +1362,7 @@ function setConsent(consentSettings) {
1326
1362
  }
1327
1363
 
1328
1364
  var name = "@firebase/analytics";
1329
- var version = "0.9.4";
1365
+ var version = "0.9.5-canary.a8d6499b1";
1330
1366
 
1331
1367
  /**
1332
1368
  * Firebase Analytics