@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.
package/dist/index.cjs.js CHANGED
@@ -5,8 +5,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var app = require('@firebase/app');
6
6
  var tslib = require('tslib');
7
7
  var logger$1 = require('@firebase/logger');
8
- var safevalues = require('safevalues');
9
- var dom = require('safevalues/dom');
10
8
  var util = require('@firebase/util');
11
9
  var component = require('@firebase/component');
12
10
  require('@firebase/installations');
@@ -56,6 +54,53 @@ var GTAG_URL = 'https://www.googletagmanager.com/gtag/js';
56
54
  */
57
55
  var logger = new logger$1.Logger('@firebase/analytics');
58
56
 
57
+ /**
58
+ * @license
59
+ * Copyright 2019 Google LLC
60
+ *
61
+ * Licensed under the Apache License, Version 2.0 (the "License");
62
+ * you may not use this file except in compliance with the License.
63
+ * You may obtain a copy of the License at
64
+ *
65
+ * http://www.apache.org/licenses/LICENSE-2.0
66
+ *
67
+ * Unless required by applicable law or agreed to in writing, software
68
+ * distributed under the License is distributed on an "AS IS" BASIS,
69
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
70
+ * See the License for the specific language governing permissions and
71
+ * limitations under the License.
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-initialized 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["no-client-id" /* AnalyticsError.NO_CLIENT_ID */] = 'The "client_id" field is empty.',
100
+ _a["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */] = 'Trusted Types detected an invalid gtag resource: {$gtagURL}.',
101
+ _a);
102
+ var ERROR_FACTORY = new util.ErrorFactory('analytics', 'Analytics', ERRORS);
103
+
59
104
  /**
60
105
  * @license
61
106
  * Copyright 2019 Google LLC
@@ -72,6 +117,19 @@ var logger = new logger$1.Logger('@firebase/analytics');
72
117
  * See the License for the specific language governing permissions and
73
118
  * limitations under the License.
74
119
  */
120
+ /**
121
+ * Verifies and creates a TrustedScriptURL.
122
+ */
123
+ function createGtagTrustedTypesScriptURL(url) {
124
+ if (!url.startsWith(GTAG_URL)) {
125
+ var err = ERROR_FACTORY.create("invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */, {
126
+ gtagURL: url
127
+ });
128
+ logger.warn(err.message);
129
+ return '';
130
+ }
131
+ return url;
132
+ }
75
133
  /**
76
134
  * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
77
135
  * have either resolved or rejected.
@@ -81,21 +139,37 @@ var logger = new logger$1.Logger('@firebase/analytics');
81
139
  function promiseAllSettled(promises) {
82
140
  return Promise.all(promises.map(function (promise) { return promise.catch(function (e) { return e; }); }));
83
141
  }
142
+ /**
143
+ * Creates a TrustedTypePolicy object that implements the rules passed as policyOptions.
144
+ *
145
+ * @param policyName A string containing the name of the policy
146
+ * @param policyOptions Object containing implementations of instance methods for TrustedTypesPolicy, see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy#instance_methods
147
+ * | the TrustedTypePolicy reference documentation}.
148
+ */
149
+ function createTrustedTypesPolicy(policyName, policyOptions) {
150
+ // Create a TrustedTypes policy that we can use for updating src
151
+ // properties
152
+ var trustedTypesPolicy;
153
+ if (window.trustedTypes) {
154
+ trustedTypesPolicy = window.trustedTypes.createPolicy(policyName, policyOptions);
155
+ }
156
+ return trustedTypesPolicy;
157
+ }
84
158
  /**
85
159
  * Inserts gtag script tag into the page to asynchronously download gtag.
86
160
  * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
87
161
  */
88
162
  function insertScriptTag(dataLayerName, measurementId) {
163
+ var trustedTypesPolicy = createTrustedTypesPolicy('firebase-js-sdk-policy', {
164
+ createScriptURL: createGtagTrustedTypesScriptURL
165
+ });
89
166
  var script = document.createElement('script');
90
167
  // We are not providing an analyticsId in the URL because it would trigger a `page_view`
91
168
  // without fid. We will initialize ga-id using gtag (config) command together with fid.
92
- //
93
- // We also have to ensure the template string before the first expression constitutes a valid URL
94
- // start, as this is what the initial validation focuses on. If the template literal begins
95
- // directly with an expression (e.g. `${GTAG_SCRIPT_URL}`), the validation fails due to an
96
- // empty initial string.
97
- var url = safevalues.trustedResourceUrl(templateObject_1 || (templateObject_1 = tslib.__makeTemplateObject(["https://www.googletagmanager.com/gtag/js?l=", "&id=", ""], ["https://www.googletagmanager.com/gtag/js?l=", "&id=", ""])), dataLayerName, measurementId);
98
- dom.safeScriptEl.setSrc(script, url);
169
+ var gtagScriptURL = "".concat(GTAG_URL, "?l=").concat(dataLayerName, "&id=").concat(measurementId);
170
+ script.src = trustedTypesPolicy
171
+ ? trustedTypesPolicy === null || trustedTypesPolicy === void 0 ? void 0 : trustedTypesPolicy.createScriptURL(gtagScriptURL)
172
+ : gtagScriptURL;
99
173
  script.async = true;
100
174
  document.head.appendChild(script);
101
175
  }
@@ -213,6 +287,7 @@ function gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPromisesL
213
287
  // if not all entries in the 'send_to' field could be mapped to
214
288
  // a FID. In these cases, wait on all pending initialization promises.
215
289
  if (initializationPromisesToWaitFor.length === 0) {
290
+ /* eslint-disable-next-line @typescript-eslint/no-floating-promises */
216
291
  initializationPromisesToWaitFor = Object.values(initializationPromisesMap);
217
292
  }
218
293
  // Run core gtag function with args after all relevant initialization
@@ -245,7 +320,7 @@ function gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPromisesL
245
320
  */
246
321
  function wrapGtag(gtagCore,
247
322
  /**
248
- * Allows wrapped gtag calls to wait on whichever intialization promises are required,
323
+ * Allows wrapped gtag calls to wait on whichever initialization promises are required,
249
324
  * depending on the contents of the gtag params' `send_to` field, if any.
250
325
  */
251
326
  initializationPromisesMap,
@@ -369,55 +444,7 @@ function findGtagScriptOnPage(dataLayerName) {
369
444
  }
370
445
  }
371
446
  return null;
372
- }
373
- var templateObject_1;
374
-
375
- /**
376
- * @license
377
- * Copyright 2019 Google LLC
378
- *
379
- * Licensed under the Apache License, Version 2.0 (the "License");
380
- * you may not use this file except in compliance with the License.
381
- * You may obtain a copy of the License at
382
- *
383
- * http://www.apache.org/licenses/LICENSE-2.0
384
- *
385
- * Unless required by applicable law or agreed to in writing, software
386
- * distributed under the License is distributed on an "AS IS" BASIS,
387
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
388
- * See the License for the specific language governing permissions and
389
- * limitations under the License.
390
- */
391
- var _a;
392
- var ERRORS = (_a = {},
393
- _a["already-exists" /* AnalyticsError.ALREADY_EXISTS */] = 'A Firebase Analytics instance with the appId {$id} ' +
394
- ' already exists. ' +
395
- 'Only one Firebase Analytics instance can be created for each appId.',
396
- _a["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */] = 'initializeAnalytics() cannot be called again with different options than those ' +
397
- 'it was initially called with. It can be called again with the same options to ' +
398
- 'return the existing instance, or getAnalytics() can be used ' +
399
- 'to get a reference to the already-intialized instance.',
400
- _a["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */] = 'Firebase Analytics has already been initialized.' +
401
- 'settings() must be called before initializing any Analytics instance' +
402
- 'or it will have no effect.',
403
- _a["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */] = 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
404
- _a["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */] = 'Firebase Analytics is not supported in this environment. ' +
405
- 'Wrap initialization of analytics in analytics.isSupported() ' +
406
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
407
- _a["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */] = 'IndexedDB unavailable or restricted in this environment. ' +
408
- 'Wrap initialization of analytics in analytics.isSupported() ' +
409
- 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
410
- _a["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */] = 'The config fetch request timed out while in an exponential backoff state.' +
411
- ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
412
- _a["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */] = 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
413
- _a["no-api-key" /* AnalyticsError.NO_API_KEY */] = 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
414
- 'contain a valid API key.',
415
- _a["no-app-id" /* AnalyticsError.NO_APP_ID */] = 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
416
- 'contain a valid app ID.',
417
- _a["no-client-id" /* AnalyticsError.NO_CLIENT_ID */] = 'The "client_id" field is empty.',
418
- _a["invalid-gtag-resource" /* AnalyticsError.INVALID_GTAG_RESOURCE */] = 'Trusted Types detected an invalid gtag resource: {$gtagURL}.',
419
- _a);
420
- var ERROR_FACTORY = new util.ErrorFactory('analytics', 'Analytics', ERRORS);
447
+ }
421
448
 
422
449
  /**
423
450
  * @license
@@ -1394,7 +1421,7 @@ function setConsent(consentSettings) {
1394
1421
  }
1395
1422
 
1396
1423
  var name = "@firebase/analytics";
1397
- var version = "0.10.6";
1424
+ var version = "0.10.7-canary.05c34c91e";
1398
1425
 
1399
1426
  /**
1400
1427
  * The Firebase Analytics Web SDK.