GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1192 lines
48 KiB

2 months ago
  1. import { _getProvider, getApp, _registerComponent, registerVersion } from '@firebase/app';
  2. import { Logger } from '@firebase/logger';
  3. import { ErrorFactory, calculateBackoffMillis, FirebaseError, isIndexedDBAvailable, validateIndexedDBOpenable, isBrowserExtension, areCookiesEnabled, getModularInstance, deepEqual } from '@firebase/util';
  4. import { Component } from '@firebase/component';
  5. import '@firebase/installations';
  6. /**
  7. * @license
  8. * Copyright 2019 Google LLC
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. /**
  23. * Type constant for Firebase Analytics.
  24. */
  25. const ANALYTICS_TYPE = 'analytics';
  26. // Key to attach FID to in gtag params.
  27. const GA_FID_KEY = 'firebase_id';
  28. const ORIGIN_KEY = 'origin';
  29. const FETCH_TIMEOUT_MILLIS = 60 * 1000;
  30. const DYNAMIC_CONFIG_URL = 'https://firebase.googleapis.com/v1alpha/projects/-/apps/{app-id}/webConfig';
  31. const GTAG_URL = 'https://www.googletagmanager.com/gtag/js';
  32. /**
  33. * @license
  34. * Copyright 2019 Google LLC
  35. *
  36. * Licensed under the Apache License, Version 2.0 (the "License");
  37. * you may not use this file except in compliance with the License.
  38. * You may obtain a copy of the License at
  39. *
  40. * http://www.apache.org/licenses/LICENSE-2.0
  41. *
  42. * Unless required by applicable law or agreed to in writing, software
  43. * distributed under the License is distributed on an "AS IS" BASIS,
  44. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  45. * See the License for the specific language governing permissions and
  46. * limitations under the License.
  47. */
  48. const logger = new Logger('@firebase/analytics');
  49. /**
  50. * @license
  51. * Copyright 2019 Google LLC
  52. *
  53. * Licensed under the Apache License, Version 2.0 (the "License");
  54. * you may not use this file except in compliance with the License.
  55. * You may obtain a copy of the License at
  56. *
  57. * http://www.apache.org/licenses/LICENSE-2.0
  58. *
  59. * Unless required by applicable law or agreed to in writing, software
  60. * distributed under the License is distributed on an "AS IS" BASIS,
  61. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  62. * See the License for the specific language governing permissions and
  63. * limitations under the License.
  64. */
  65. /**
  66. * Makeshift polyfill for Promise.allSettled(). Resolves when all promises
  67. * have either resolved or rejected.
  68. *
  69. * @param promises Array of promises to wait for.
  70. */
  71. function promiseAllSettled(promises) {
  72. return Promise.all(promises.map(promise => promise.catch(e => e)));
  73. }
  74. /**
  75. * Inserts gtag script tag into the page to asynchronously download gtag.
  76. * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
  77. */
  78. function insertScriptTag(dataLayerName, measurementId) {
  79. const script = document.createElement('script');
  80. // We are not providing an analyticsId in the URL because it would trigger a `page_view`
  81. // without fid. We will initialize ga-id using gtag (config) command together with fid.
  82. script.src = `${GTAG_URL}?l=${dataLayerName}&id=${measurementId}`;
  83. script.async = true;
  84. document.head.appendChild(script);
  85. }
  86. /**
  87. * Get reference to, or create, global datalayer.
  88. * @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
  89. */
  90. function getOrCreateDataLayer(dataLayerName) {
  91. // Check for existing dataLayer and create if needed.
  92. let dataLayer = [];
  93. if (Array.isArray(window[dataLayerName])) {
  94. dataLayer = window[dataLayerName];
  95. }
  96. else {
  97. window[dataLayerName] = dataLayer;
  98. }
  99. return dataLayer;
  100. }
  101. /**
  102. * Wrapped gtag logic when gtag is called with 'config' command.
  103. *
  104. * @param gtagCore Basic gtag function that just appends to dataLayer.
  105. * @param initializationPromisesMap Map of appIds to their initialization promises.
  106. * @param dynamicConfigPromisesList Array of dynamic config fetch promises.
  107. * @param measurementIdToAppId Map of GA measurementIDs to corresponding Firebase appId.
  108. * @param measurementId GA Measurement ID to set config for.
  109. * @param gtagParams Gtag config params to set.
  110. */
  111. async function gtagOnConfig(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId, measurementId, gtagParams) {
  112. // If config is already fetched, we know the appId and can use it to look up what FID promise we
  113. /// are waiting for, and wait only on that one.
  114. const correspondingAppId = measurementIdToAppId[measurementId];
  115. try {
  116. if (correspondingAppId) {
  117. await initializationPromisesMap[correspondingAppId];
  118. }
  119. else {
  120. // If config is not fetched yet, wait for all configs (we don't know which one we need) and
  121. // find the appId (if any) corresponding to this measurementId. If there is one, wait on
  122. // that appId's initialization promise. If there is none, promise resolves and gtag
  123. // call goes through.
  124. const dynamicConfigResults = await promiseAllSettled(dynamicConfigPromisesList);
  125. const foundConfig = dynamicConfigResults.find(config => config.measurementId === measurementId);
  126. if (foundConfig) {
  127. await initializationPromisesMap[foundConfig.appId];
  128. }
  129. }
  130. }
  131. catch (e) {
  132. logger.error(e);
  133. }
  134. gtagCore("config" /* GtagCommand.CONFIG */, measurementId, gtagParams);
  135. }
  136. /**
  137. * Wrapped gtag logic when gtag is called with 'event' command.
  138. *
  139. * @param gtagCore Basic gtag function that just appends to dataLayer.
  140. * @param initializationPromisesMap Map of appIds to their initialization promises.
  141. * @param dynamicConfigPromisesList Array of dynamic config fetch promises.
  142. * @param measurementId GA Measurement ID to log event to.
  143. * @param gtagParams Params to log with this event.
  144. */
  145. async function gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, measurementId, gtagParams) {
  146. try {
  147. let initializationPromisesToWaitFor = [];
  148. // If there's a 'send_to' param, check if any ID specified matches
  149. // an initializeIds() promise we are waiting for.
  150. if (gtagParams && gtagParams['send_to']) {
  151. let gaSendToList = gtagParams['send_to'];
  152. // Make it an array if is isn't, so it can be dealt with the same way.
  153. if (!Array.isArray(gaSendToList)) {
  154. gaSendToList = [gaSendToList];
  155. }
  156. // Checking 'send_to' fields requires having all measurement ID results back from
  157. // the dynamic config fetch.
  158. const dynamicConfigResults = await promiseAllSettled(dynamicConfigPromisesList);
  159. for (const sendToId of gaSendToList) {
  160. // Any fetched dynamic measurement ID that matches this 'send_to' ID
  161. const foundConfig = dynamicConfigResults.find(config => config.measurementId === sendToId);
  162. const initializationPromise = foundConfig && initializationPromisesMap[foundConfig.appId];
  163. if (initializationPromise) {
  164. initializationPromisesToWaitFor.push(initializationPromise);
  165. }
  166. else {
  167. // Found an item in 'send_to' that is not associated
  168. // directly with an FID, possibly a group. Empty this array,
  169. // exit the loop early, and let it get populated below.
  170. initializationPromisesToWaitFor = [];
  171. break;
  172. }
  173. }
  174. }
  175. // This will be unpopulated if there was no 'send_to' field , or
  176. // if not all entries in the 'send_to' field could be mapped to
  177. // a FID. In these cases, wait on all pending initialization promises.
  178. if (initializationPromisesToWaitFor.length === 0) {
  179. initializationPromisesToWaitFor = Object.values(initializationPromisesMap);
  180. }
  181. // Run core gtag function with args after all relevant initialization
  182. // promises have been resolved.
  183. await Promise.all(initializationPromisesToWaitFor);
  184. // Workaround for http://b/141370449 - third argument cannot be undefined.
  185. gtagCore("event" /* GtagCommand.EVENT */, measurementId, gtagParams || {});
  186. }
  187. catch (e) {
  188. logger.error(e);
  189. }
  190. }
  191. /**
  192. * Wraps a standard gtag function with extra code to wait for completion of
  193. * relevant initialization promises before sending requests.
  194. *
  195. * @param gtagCore Basic gtag function that just appends to dataLayer.
  196. * @param initializationPromisesMap Map of appIds to their initialization promises.
  197. * @param dynamicConfigPromisesList Array of dynamic config fetch promises.
  198. * @param measurementIdToAppId Map of GA measurementIDs to corresponding Firebase appId.
  199. */
  200. function wrapGtag(gtagCore,
  201. /**
  202. * Allows wrapped gtag calls to wait on whichever intialization promises are required,
  203. * depending on the contents of the gtag params' `send_to` field, if any.
  204. */
  205. initializationPromisesMap,
  206. /**
  207. * Wrapped gtag calls sometimes require all dynamic config fetches to have returned
  208. * before determining what initialization promises (which include FIDs) to wait for.
  209. */
  210. dynamicConfigPromisesList,
  211. /**
  212. * Wrapped gtag config calls can narrow down which initialization promise (with FID)
  213. * to wait for if the measurementId is already fetched, by getting the corresponding appId,
  214. * which is the key for the initialization promises map.
  215. */
  216. measurementIdToAppId) {
  217. /**
  218. * Wrapper around gtag that ensures FID is sent with gtag calls.
  219. * @param command Gtag command type.
  220. * @param idOrNameOrParams Measurement ID if command is EVENT/CONFIG, params if command is SET.
  221. * @param gtagParams Params if event is EVENT/CONFIG.
  222. */
  223. async function gtagWrapper(command, idOrNameOrParams, gtagParams) {
  224. try {
  225. // If event, check that relevant initialization promises have completed.
  226. if (command === "event" /* GtagCommand.EVENT */) {
  227. // If EVENT, second arg must be measurementId.
  228. await gtagOnEvent(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, idOrNameOrParams, gtagParams);
  229. }
  230. else if (command === "config" /* GtagCommand.CONFIG */) {
  231. // If CONFIG, second arg must be measurementId.
  232. await gtagOnConfig(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId, idOrNameOrParams, gtagParams);
  233. }
  234. else if (command === "consent" /* GtagCommand.CONSENT */) {
  235. // If CONFIG, second arg must be measurementId.
  236. gtagCore("consent" /* GtagCommand.CONSENT */, 'update', gtagParams);
  237. }
  238. else {
  239. // If SET, second arg must be params.
  240. gtagCore("set" /* GtagCommand.SET */, idOrNameOrParams);
  241. }
  242. }
  243. catch (e) {
  244. logger.error(e);
  245. }
  246. }
  247. return gtagWrapper;
  248. }
  249. /**
  250. * Creates global gtag function or wraps existing one if found.
  251. * This wrapped function attaches Firebase instance ID (FID) to gtag 'config' and
  252. * 'event' calls that belong to the GAID associated with this Firebase instance.
  253. *
  254. * @param initializationPromisesMap Map of appIds to their initialization promises.
  255. * @param dynamicConfigPromisesList Array of dynamic config fetch promises.
  256. * @param measurementIdToAppId Map of GA measurementIDs to corresponding Firebase appId.
  257. * @param dataLayerName Name of global GA datalayer array.
  258. * @param gtagFunctionName Name of global gtag function ("gtag" if not user-specified).
  259. */
  260. function wrapOrCreateGtag(initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId, dataLayerName, gtagFunctionName) {
  261. // Create a basic core gtag function
  262. let gtagCore = function (..._args) {
  263. // Must push IArguments object, not an array.
  264. window[dataLayerName].push(arguments);
  265. };
  266. // Replace it with existing one if found
  267. if (window[gtagFunctionName] &&
  268. typeof window[gtagFunctionName] === 'function') {
  269. // @ts-ignore
  270. gtagCore = window[gtagFunctionName];
  271. }
  272. window[gtagFunctionName] = wrapGtag(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId);
  273. return {
  274. gtagCore,
  275. wrappedGtag: window[gtagFunctionName]
  276. };
  277. }
  278. /**
  279. * Returns the script tag in the DOM matching both the gtag url pattern
  280. * and the provided data layer name.
  281. */
  282. function findGtagScriptOnPage(dataLayerName) {
  283. const scriptTags = window.document.getElementsByTagName('script');
  284. for (const tag of Object.values(scriptTags)) {
  285. if (tag.src &&
  286. tag.src.includes(GTAG_URL) &&
  287. tag.src.includes(dataLayerName)) {
  288. return tag;
  289. }
  290. }
  291. return null;
  292. }
  293. /**
  294. * @license
  295. * Copyright 2019 Google LLC
  296. *
  297. * Licensed under the Apache License, Version 2.0 (the "License");
  298. * you may not use this file except in compliance with the License.
  299. * You may obtain a copy of the License at
  300. *
  301. * http://www.apache.org/licenses/LICENSE-2.0
  302. *
  303. * Unless required by applicable law or agreed to in writing, software
  304. * distributed under the License is distributed on an "AS IS" BASIS,
  305. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  306. * See the License for the specific language governing permissions and
  307. * limitations under the License.
  308. */
  309. const ERRORS = {
  310. ["already-exists" /* AnalyticsError.ALREADY_EXISTS */]: 'A Firebase Analytics instance with the appId {$id} ' +
  311. ' already exists. ' +
  312. 'Only one Firebase Analytics instance can be created for each appId.',
  313. ["already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */]: 'initializeAnalytics() cannot be called again with different options than those ' +
  314. 'it was initially called with. It can be called again with the same options to ' +
  315. 'return the existing instance, or getAnalytics() can be used ' +
  316. 'to get a reference to the already-intialized instance.',
  317. ["already-initialized-settings" /* AnalyticsError.ALREADY_INITIALIZED_SETTINGS */]: 'Firebase Analytics has already been initialized.' +
  318. 'settings() must be called before initializing any Analytics instance' +
  319. 'or it will have no effect.',
  320. ["interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */]: 'Firebase Analytics Interop Component failed to instantiate: {$reason}',
  321. ["invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */]: 'Firebase Analytics is not supported in this environment. ' +
  322. 'Wrap initialization of analytics in analytics.isSupported() ' +
  323. 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
  324. ["indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */]: 'IndexedDB unavailable or restricted in this environment. ' +
  325. 'Wrap initialization of analytics in analytics.isSupported() ' +
  326. 'to prevent initialization in unsupported environments. Details: {$errorInfo}',
  327. ["fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */]: 'The config fetch request timed out while in an exponential backoff state.' +
  328. ' Unix timestamp in milliseconds when fetch request throttling ends: {$throttleEndTimeMillis}.',
  329. ["config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */]: 'Dynamic config fetch failed: [{$httpStatus}] {$responseMessage}',
  330. ["no-api-key" /* AnalyticsError.NO_API_KEY */]: 'The "apiKey" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
  331. 'contain a valid API key.',
  332. ["no-app-id" /* AnalyticsError.NO_APP_ID */]: 'The "appId" field is empty in the local Firebase config. Firebase Analytics requires this field to' +
  333. 'contain a valid app ID.'
  334. };
  335. const ERROR_FACTORY = new ErrorFactory('analytics', 'Analytics', ERRORS);
  336. /**
  337. * @license
  338. * Copyright 2020 Google LLC
  339. *
  340. * Licensed under the Apache License, Version 2.0 (the "License");
  341. * you may not use this file except in compliance with the License.
  342. * You may obtain a copy of the License at
  343. *
  344. * http://www.apache.org/licenses/LICENSE-2.0
  345. *
  346. * Unless required by applicable law or agreed to in writing, software
  347. * distributed under the License is distributed on an "AS IS" BASIS,
  348. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  349. * See the License for the specific language governing permissions and
  350. * limitations under the License.
  351. */
  352. /**
  353. * Backoff factor for 503 errors, which we want to be conservative about
  354. * to avoid overloading servers. Each retry interval will be
  355. * BASE_INTERVAL_MILLIS * LONG_RETRY_FACTOR ^ retryCount, so the second one
  356. * will be ~30 seconds (with fuzzing).
  357. */
  358. const LONG_RETRY_FACTOR = 30;
  359. /**
  360. * Base wait interval to multiplied by backoffFactor^backoffCount.
  361. */
  362. const BASE_INTERVAL_MILLIS = 1000;
  363. /**
  364. * Stubbable retry data storage class.
  365. */
  366. class RetryData {
  367. constructor(throttleMetadata = {}, intervalMillis = BASE_INTERVAL_MILLIS) {
  368. this.throttleMetadata = throttleMetadata;
  369. this.intervalMillis = intervalMillis;
  370. }
  371. getThrottleMetadata(appId) {
  372. return this.throttleMetadata[appId];
  373. }
  374. setThrottleMetadata(appId, metadata) {
  375. this.throttleMetadata[appId] = metadata;
  376. }
  377. deleteThrottleMetadata(appId) {
  378. delete this.throttleMetadata[appId];
  379. }
  380. }
  381. const defaultRetryData = new RetryData();
  382. /**
  383. * Set GET request headers.
  384. * @param apiKey App API key.
  385. */
  386. function getHeaders(apiKey) {
  387. return new Headers({
  388. Accept: 'application/json',
  389. 'x-goog-api-key': apiKey
  390. });
  391. }
  392. /**
  393. * Fetches dynamic config from backend.
  394. * @param app Firebase app to fetch config for.
  395. */
  396. async function fetchDynamicConfig(appFields) {
  397. var _a;
  398. const { appId, apiKey } = appFields;
  399. const request = {
  400. method: 'GET',
  401. headers: getHeaders(apiKey)
  402. };
  403. const appUrl = DYNAMIC_CONFIG_URL.replace('{app-id}', appId);
  404. const response = await fetch(appUrl, request);
  405. if (response.status !== 200 && response.status !== 304) {
  406. let errorMessage = '';
  407. try {
  408. // Try to get any error message text from server response.
  409. const jsonResponse = (await response.json());
  410. if ((_a = jsonResponse.error) === null || _a === void 0 ? void 0 : _a.message) {
  411. errorMessage = jsonResponse.error.message;
  412. }
  413. }
  414. catch (_ignored) { }
  415. throw ERROR_FACTORY.create("config-fetch-failed" /* AnalyticsError.CONFIG_FETCH_FAILED */, {
  416. httpStatus: response.status,
  417. responseMessage: errorMessage
  418. });
  419. }
  420. return response.json();
  421. }
  422. /**
  423. * Fetches dynamic config from backend, retrying if failed.
  424. * @param app Firebase app to fetch config for.
  425. */
  426. async function fetchDynamicConfigWithRetry(app,
  427. // retryData and timeoutMillis are parameterized to allow passing a different value for testing.
  428. retryData = defaultRetryData, timeoutMillis) {
  429. const { appId, apiKey, measurementId } = app.options;
  430. if (!appId) {
  431. throw ERROR_FACTORY.create("no-app-id" /* AnalyticsError.NO_APP_ID */);
  432. }
  433. if (!apiKey) {
  434. if (measurementId) {
  435. return {
  436. measurementId,
  437. appId
  438. };
  439. }
  440. throw ERROR_FACTORY.create("no-api-key" /* AnalyticsError.NO_API_KEY */);
  441. }
  442. const throttleMetadata = retryData.getThrottleMetadata(appId) || {
  443. backoffCount: 0,
  444. throttleEndTimeMillis: Date.now()
  445. };
  446. const signal = new AnalyticsAbortSignal();
  447. setTimeout(async () => {
  448. // Note a very low delay, eg < 10ms, can elapse before listeners are initialized.
  449. signal.abort();
  450. }, timeoutMillis !== undefined ? timeoutMillis : FETCH_TIMEOUT_MILLIS);
  451. return attemptFetchDynamicConfigWithRetry({ appId, apiKey, measurementId }, throttleMetadata, signal, retryData);
  452. }
  453. /**
  454. * Runs one retry attempt.
  455. * @param appFields Necessary app config fields.
  456. * @param throttleMetadata Ongoing metadata to determine throttling times.
  457. * @param signal Abort signal.
  458. */
  459. async function attemptFetchDynamicConfigWithRetry(appFields, { throttleEndTimeMillis, backoffCount }, signal, retryData = defaultRetryData // for testing
  460. ) {
  461. var _a;
  462. const { appId, measurementId } = appFields;
  463. // Starts with a (potentially zero) timeout to support resumption from stored state.
  464. // Ensures the throttle end time is honored if the last attempt timed out.
  465. // Note the SDK will never make a request if the fetch timeout expires at this point.
  466. try {
  467. await setAbortableTimeout(signal, throttleEndTimeMillis);
  468. }
  469. catch (e) {
  470. if (measurementId) {
  471. logger.warn(`Timed out fetching this Firebase app's measurement ID from the server.` +
  472. ` Falling back to the measurement ID ${measurementId}` +
  473. ` provided in the "measurementId" field in the local Firebase config. [${e === null || e === void 0 ? void 0 : e.message}]`);
  474. return { appId, measurementId };
  475. }
  476. throw e;
  477. }
  478. try {
  479. const response = await fetchDynamicConfig(appFields);
  480. // Note the SDK only clears throttle state if response is success or non-retriable.
  481. retryData.deleteThrottleMetadata(appId);
  482. return response;
  483. }
  484. catch (e) {
  485. const error = e;
  486. if (!isRetriableError(error)) {
  487. retryData.deleteThrottleMetadata(appId);
  488. if (measurementId) {
  489. logger.warn(`Failed to fetch this Firebase app's measurement ID from the server.` +
  490. ` Falling back to the measurement ID ${measurementId}` +
  491. ` provided in the "measurementId" field in the local Firebase config. [${error === null || error === void 0 ? void 0 : error.message}]`);
  492. return { appId, measurementId };
  493. }
  494. else {
  495. throw e;
  496. }
  497. }
  498. const backoffMillis = Number((_a = error === null || error === void 0 ? void 0 : error.customData) === null || _a === void 0 ? void 0 : _a.httpStatus) === 503
  499. ? calculateBackoffMillis(backoffCount, retryData.intervalMillis, LONG_RETRY_FACTOR)
  500. : calculateBackoffMillis(backoffCount, retryData.intervalMillis);
  501. // Increments backoff state.
  502. const throttleMetadata = {
  503. throttleEndTimeMillis: Date.now() + backoffMillis,
  504. backoffCount: backoffCount + 1
  505. };
  506. // Persists state.
  507. retryData.setThrottleMetadata(appId, throttleMetadata);
  508. logger.debug(`Calling attemptFetch again in ${backoffMillis} millis`);
  509. return attemptFetchDynamicConfigWithRetry(appFields, throttleMetadata, signal, retryData);
  510. }
  511. }
  512. /**
  513. * Supports waiting on a backoff by:
  514. *
  515. * <ul>
  516. * <li>Promisifying setTimeout, so we can set a timeout in our Promise chain</li>
  517. * <li>Listening on a signal bus for abort events, just like the Fetch API</li>
  518. * <li>Failing in the same way the Fetch API fails, so timing out a live request and a throttled
  519. * request appear the same.</li>
  520. * </ul>
  521. *
  522. * <p>Visible for testing.
  523. */
  524. function setAbortableTimeout(signal, throttleEndTimeMillis) {
  525. return new Promise((resolve, reject) => {
  526. // Derives backoff from given end time, normalizing negative numbers to zero.
  527. const backoffMillis = Math.max(throttleEndTimeMillis - Date.now(), 0);
  528. const timeout = setTimeout(resolve, backoffMillis);
  529. // Adds listener, rather than sets onabort, because signal is a shared object.
  530. signal.addEventListener(() => {
  531. clearTimeout(timeout);
  532. // If the request completes before this timeout, the rejection has no effect.
  533. reject(ERROR_FACTORY.create("fetch-throttle" /* AnalyticsError.FETCH_THROTTLE */, {
  534. throttleEndTimeMillis
  535. }));
  536. });
  537. });
  538. }
  539. /**
  540. * Returns true if the {@link Error} indicates a fetch request may succeed later.
  541. */
  542. function isRetriableError(e) {
  543. if (!(e instanceof FirebaseError) || !e.customData) {
  544. return false;
  545. }
  546. // Uses string index defined by ErrorData, which FirebaseError implements.
  547. const httpStatus = Number(e.customData['httpStatus']);
  548. return (httpStatus === 429 ||
  549. httpStatus === 500 ||
  550. httpStatus === 503 ||
  551. httpStatus === 504);
  552. }
  553. /**
  554. * Shims a minimal AbortSignal (copied from Remote Config).
  555. *
  556. * <p>AbortController's AbortSignal conveniently decouples fetch timeout logic from other aspects
  557. * of networking, such as retries. Firebase doesn't use AbortController enough to justify a
  558. * polyfill recommendation, like we do with the Fetch API, but this minimal shim can easily be
  559. * swapped out if/when we do.
  560. */
  561. class AnalyticsAbortSignal {
  562. constructor() {
  563. this.listeners = [];
  564. }
  565. addEventListener(listener) {
  566. this.listeners.push(listener);
  567. }
  568. abort() {
  569. this.listeners.forEach(listener => listener());
  570. }
  571. }
  572. /**
  573. * @license
  574. * Copyright 2019 Google LLC
  575. *
  576. * Licensed under the Apache License, Version 2.0 (the "License");
  577. * you may not use this file except in compliance with the License.
  578. * You may obtain a copy of the License at
  579. *
  580. * http://www.apache.org/licenses/LICENSE-2.0
  581. *
  582. * Unless required by applicable law or agreed to in writing, software
  583. * distributed under the License is distributed on an "AS IS" BASIS,
  584. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  585. * See the License for the specific language governing permissions and
  586. * limitations under the License.
  587. */
  588. /**
  589. * Event parameters to set on 'gtag' during initialization.
  590. */
  591. let defaultEventParametersForInit;
  592. /**
  593. * Logs an analytics event through the Firebase SDK.
  594. *
  595. * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
  596. * @param eventName Google Analytics event name, choose from standard list or use a custom string.
  597. * @param eventParams Analytics event parameters.
  598. */
  599. async function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
  600. if (options && options.global) {
  601. gtagFunction("event" /* GtagCommand.EVENT */, eventName, eventParams);
  602. return;
  603. }
  604. else {
  605. const measurementId = await initializationPromise;
  606. const params = Object.assign(Object.assign({}, eventParams), { 'send_to': measurementId });
  607. gtagFunction("event" /* GtagCommand.EVENT */, eventName, params);
  608. }
  609. }
  610. /**
  611. * Set screen_name parameter for this Google Analytics ID.
  612. *
  613. * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
  614. * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
  615. *
  616. * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
  617. * @param screenName Screen name string to set.
  618. */
  619. async function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
  620. if (options && options.global) {
  621. gtagFunction("set" /* GtagCommand.SET */, { 'screen_name': screenName });
  622. return Promise.resolve();
  623. }
  624. else {
  625. const measurementId = await initializationPromise;
  626. gtagFunction("config" /* GtagCommand.CONFIG */, measurementId, {
  627. update: true,
  628. 'screen_name': screenName
  629. });
  630. }
  631. }
  632. /**
  633. * Set user_id parameter for this Google Analytics ID.
  634. *
  635. * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
  636. * @param id User ID string to set
  637. */
  638. async function setUserId$1(gtagFunction, initializationPromise, id, options) {
  639. if (options && options.global) {
  640. gtagFunction("set" /* GtagCommand.SET */, { 'user_id': id });
  641. return Promise.resolve();
  642. }
  643. else {
  644. const measurementId = await initializationPromise;
  645. gtagFunction("config" /* GtagCommand.CONFIG */, measurementId, {
  646. update: true,
  647. 'user_id': id
  648. });
  649. }
  650. }
  651. /**
  652. * Set all other user properties other than user_id and screen_name.
  653. *
  654. * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
  655. * @param properties Map of user properties to set
  656. */
  657. async function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
  658. if (options && options.global) {
  659. const flatProperties = {};
  660. for (const key of Object.keys(properties)) {
  661. // use dot notation for merge behavior in gtag.js
  662. flatProperties[`user_properties.${key}`] = properties[key];
  663. }
  664. gtagFunction("set" /* GtagCommand.SET */, flatProperties);
  665. return Promise.resolve();
  666. }
  667. else {
  668. const measurementId = await initializationPromise;
  669. gtagFunction("config" /* GtagCommand.CONFIG */, measurementId, {
  670. update: true,
  671. 'user_properties': properties
  672. });
  673. }
  674. }
  675. /**
  676. * Set whether collection is enabled for this ID.
  677. *
  678. * @param enabled If true, collection is enabled for this ID.
  679. */
  680. async function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
  681. const measurementId = await initializationPromise;
  682. window[`ga-disable-${measurementId}`] = !enabled;
  683. }
  684. /**
  685. * Consent parameters to default to during 'gtag' initialization.
  686. */
  687. let defaultConsentSettingsForInit;
  688. /**
  689. * Sets the variable {@link defaultConsentSettingsForInit} for use in the initialization of
  690. * analytics.
  691. *
  692. * @param consentSettings Maps the applicable end user consent state for gtag.js.
  693. */
  694. function _setConsentDefaultForInit(consentSettings) {
  695. defaultConsentSettingsForInit = consentSettings;
  696. }
  697. /**
  698. * Sets the variable `defaultEventParametersForInit` for use in the initialization of
  699. * analytics.
  700. *
  701. * @param customParams Any custom params the user may pass to gtag.js.
  702. */
  703. function _setDefaultEventParametersForInit(customParams) {
  704. defaultEventParametersForInit = customParams;
  705. }
  706. /**
  707. * @license
  708. * Copyright 2020 Google LLC
  709. *
  710. * Licensed under the Apache License, Version 2.0 (the "License");
  711. * you may not use this file except in compliance with the License.
  712. * You may obtain a copy of the License at
  713. *
  714. * http://www.apache.org/licenses/LICENSE-2.0
  715. *
  716. * Unless required by applicable law or agreed to in writing, software
  717. * distributed under the License is distributed on an "AS IS" BASIS,
  718. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  719. * See the License for the specific language governing permissions and
  720. * limitations under the License.
  721. */
  722. async function validateIndexedDB() {
  723. if (!isIndexedDBAvailable()) {
  724. logger.warn(ERROR_FACTORY.create("indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */, {
  725. errorInfo: 'IndexedDB is not available in this environment.'
  726. }).message);
  727. return false;
  728. }
  729. else {
  730. try {
  731. await validateIndexedDBOpenable();
  732. }
  733. catch (e) {
  734. logger.warn(ERROR_FACTORY.create("indexeddb-unavailable" /* AnalyticsError.INDEXEDDB_UNAVAILABLE */, {
  735. errorInfo: e === null || e === void 0 ? void 0 : e.toString()
  736. }).message);
  737. return false;
  738. }
  739. }
  740. return true;
  741. }
  742. /**
  743. * Initialize the analytics instance in gtag.js by calling config command with fid.
  744. *
  745. * NOTE: We combine analytics initialization and setting fid together because we want fid to be
  746. * part of the `page_view` event that's sent during the initialization
  747. * @param app Firebase app
  748. * @param gtagCore The gtag function that's not wrapped.
  749. * @param dynamicConfigPromisesList Array of all dynamic config promises.
  750. * @param measurementIdToAppId Maps measurementID to appID.
  751. * @param installations _FirebaseInstallationsInternal instance.
  752. *
  753. * @returns Measurement ID.
  754. */
  755. async function _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToAppId, installations, gtagCore, dataLayerName, options) {
  756. var _a;
  757. const dynamicConfigPromise = fetchDynamicConfigWithRetry(app);
  758. // Once fetched, map measurementIds to appId, for ease of lookup in wrapped gtag function.
  759. dynamicConfigPromise
  760. .then(config => {
  761. measurementIdToAppId[config.measurementId] = config.appId;
  762. if (app.options.measurementId &&
  763. config.measurementId !== app.options.measurementId) {
  764. logger.warn(`The measurement ID in the local Firebase config (${app.options.measurementId})` +
  765. ` does not match the measurement ID fetched from the server (${config.measurementId}).` +
  766. ` To ensure analytics events are always sent to the correct Analytics property,` +
  767. ` update the` +
  768. ` measurement ID field in the local config or remove it from the local config.`);
  769. }
  770. })
  771. .catch(e => logger.error(e));
  772. // Add to list to track state of all dynamic config promises.
  773. dynamicConfigPromisesList.push(dynamicConfigPromise);
  774. const fidPromise = validateIndexedDB().then(envIsValid => {
  775. if (envIsValid) {
  776. return installations.getId();
  777. }
  778. else {
  779. return undefined;
  780. }
  781. });
  782. const [dynamicConfig, fid] = await Promise.all([
  783. dynamicConfigPromise,
  784. fidPromise
  785. ]);
  786. // Detect if user has already put the gtag <script> tag on this page with the passed in
  787. // data layer name.
  788. if (!findGtagScriptOnPage(dataLayerName)) {
  789. insertScriptTag(dataLayerName, dynamicConfig.measurementId);
  790. }
  791. // Detects if there are consent settings that need to be configured.
  792. if (defaultConsentSettingsForInit) {
  793. gtagCore("consent" /* GtagCommand.CONSENT */, 'default', defaultConsentSettingsForInit);
  794. _setConsentDefaultForInit(undefined);
  795. }
  796. // This command initializes gtag.js and only needs to be called once for the entire web app,
  797. // but since it is idempotent, we can call it multiple times.
  798. // We keep it together with other initialization logic for better code structure.
  799. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  800. gtagCore('js', new Date());
  801. // User config added first. We don't want users to accidentally overwrite
  802. // base Firebase config properties.
  803. const configProperties = (_a = options === null || options === void 0 ? void 0 : options.config) !== null && _a !== void 0 ? _a : {};
  804. // guard against developers accidentally setting properties with prefix `firebase_`
  805. configProperties[ORIGIN_KEY] = 'firebase';
  806. configProperties.update = true;
  807. if (fid != null) {
  808. configProperties[GA_FID_KEY] = fid;
  809. }
  810. // It should be the first config command called on this GA-ID
  811. // Initialize this GA-ID and set FID on it using the gtag config API.
  812. // Note: This will trigger a page_view event unless 'send_page_view' is set to false in
  813. // `configProperties`.
  814. gtagCore("config" /* GtagCommand.CONFIG */, dynamicConfig.measurementId, configProperties);
  815. // Detects if there is data that will be set on every event logged from the SDK.
  816. if (defaultEventParametersForInit) {
  817. gtagCore("set" /* GtagCommand.SET */, defaultEventParametersForInit);
  818. _setDefaultEventParametersForInit(undefined);
  819. }
  820. return dynamicConfig.measurementId;
  821. }
  822. /**
  823. * @license
  824. * Copyright 2019 Google LLC
  825. *
  826. * Licensed under the Apache License, Version 2.0 (the "License");
  827. * you may not use this file except in compliance with the License.
  828. * You may obtain a copy of the License at
  829. *
  830. * http://www.apache.org/licenses/LICENSE-2.0
  831. *
  832. * Unless required by applicable law or agreed to in writing, software
  833. * distributed under the License is distributed on an "AS IS" BASIS,
  834. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  835. * See the License for the specific language governing permissions and
  836. * limitations under the License.
  837. */
  838. /**
  839. * Analytics Service class.
  840. */
  841. class AnalyticsService {
  842. constructor(app) {
  843. this.app = app;
  844. }
  845. _delete() {
  846. delete initializationPromisesMap[this.app.options.appId];
  847. return Promise.resolve();
  848. }
  849. }
  850. /**
  851. * Maps appId to full initialization promise. Wrapped gtag calls must wait on
  852. * all or some of these, depending on the call's `send_to` param and the status
  853. * of the dynamic config fetches (see below).
  854. */
  855. let initializationPromisesMap = {};
  856. /**
  857. * List of dynamic config fetch promises. In certain cases, wrapped gtag calls
  858. * wait on all these to be complete in order to determine if it can selectively
  859. * wait for only certain initialization (FID) promises or if it must wait for all.
  860. */
  861. let dynamicConfigPromisesList = [];
  862. /**
  863. * Maps fetched measurementIds to appId. Populated when the app's dynamic config
  864. * fetch completes. If already populated, gtag config calls can use this to
  865. * selectively wait for only this app's initialization promise (FID) instead of all
  866. * initialization promises.
  867. */
  868. const measurementIdToAppId = {};
  869. /**
  870. * Name for window global data layer array used by GA: defaults to 'dataLayer'.
  871. */
  872. let dataLayerName = 'dataLayer';
  873. /**
  874. * Name for window global gtag function used by GA: defaults to 'gtag'.
  875. */
  876. let gtagName = 'gtag';
  877. /**
  878. * Reproduction of standard gtag function or reference to existing
  879. * gtag function on window object.
  880. */
  881. let gtagCoreFunction;
  882. /**
  883. * Wrapper around gtag function that ensures FID is sent with all
  884. * relevant event and config calls.
  885. */
  886. let wrappedGtagFunction;
  887. /**
  888. * Flag to ensure page initialization steps (creation or wrapping of
  889. * dataLayer and gtag script) are only run once per page load.
  890. */
  891. let globalInitDone = false;
  892. /**
  893. * Configures Firebase Analytics to use custom `gtag` or `dataLayer` names.
  894. * Intended to be used if `gtag.js` script has been installed on
  895. * this page independently of Firebase Analytics, and is using non-default
  896. * names for either the `gtag` function or for `dataLayer`.
  897. * Must be called before calling `getAnalytics()` or it won't
  898. * have any effect.
  899. *
  900. * @public
  901. *
  902. * @param options - Custom gtag and dataLayer names.
  903. */
  904. function settings(options) {
  905. if (globalInitDone) {
  906. throw ERROR_FACTORY.create("already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */);
  907. }
  908. if (options.dataLayerName) {
  909. dataLayerName = options.dataLayerName;
  910. }
  911. if (options.gtagName) {
  912. gtagName = options.gtagName;
  913. }
  914. }
  915. /**
  916. * Returns true if no environment mismatch is found.
  917. * If environment mismatches are found, throws an INVALID_ANALYTICS_CONTEXT
  918. * error that also lists details for each mismatch found.
  919. */
  920. function warnOnBrowserContextMismatch() {
  921. const mismatchedEnvMessages = [];
  922. if (isBrowserExtension()) {
  923. mismatchedEnvMessages.push('This is a browser extension environment.');
  924. }
  925. if (!areCookiesEnabled()) {
  926. mismatchedEnvMessages.push('Cookies are not available.');
  927. }
  928. if (mismatchedEnvMessages.length > 0) {
  929. const details = mismatchedEnvMessages
  930. .map((message, index) => `(${index + 1}) ${message}`)
  931. .join(' ');
  932. const err = ERROR_FACTORY.create("invalid-analytics-context" /* AnalyticsError.INVALID_ANALYTICS_CONTEXT */, {
  933. errorInfo: details
  934. });
  935. logger.warn(err.message);
  936. }
  937. }
  938. /**
  939. * Analytics instance factory.
  940. * @internal
  941. */
  942. function factory(app, installations, options) {
  943. warnOnBrowserContextMismatch();
  944. const appId = app.options.appId;
  945. if (!appId) {
  946. throw ERROR_FACTORY.create("no-app-id" /* AnalyticsError.NO_APP_ID */);
  947. }
  948. if (!app.options.apiKey) {
  949. if (app.options.measurementId) {
  950. logger.warn(`The "apiKey" field is empty in the local Firebase config. This is needed to fetch the latest` +
  951. ` measurement ID for this Firebase app. Falling back to the measurement ID ${app.options.measurementId}` +
  952. ` provided in the "measurementId" field in the local Firebase config.`);
  953. }
  954. else {
  955. throw ERROR_FACTORY.create("no-api-key" /* AnalyticsError.NO_API_KEY */);
  956. }
  957. }
  958. if (initializationPromisesMap[appId] != null) {
  959. throw ERROR_FACTORY.create("already-exists" /* AnalyticsError.ALREADY_EXISTS */, {
  960. id: appId
  961. });
  962. }
  963. if (!globalInitDone) {
  964. // Steps here should only be done once per page: creation or wrapping
  965. // of dataLayer and global gtag function.
  966. getOrCreateDataLayer(dataLayerName);
  967. const { wrappedGtag, gtagCore } = wrapOrCreateGtag(initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId, dataLayerName, gtagName);
  968. wrappedGtagFunction = wrappedGtag;
  969. gtagCoreFunction = gtagCore;
  970. globalInitDone = true;
  971. }
  972. // Async but non-blocking.
  973. // This map reflects the completion state of all promises for each appId.
  974. initializationPromisesMap[appId] = _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToAppId, installations, gtagCoreFunction, dataLayerName, options);
  975. const analyticsInstance = new AnalyticsService(app);
  976. return analyticsInstance;
  977. }
  978. /* eslint-disable @typescript-eslint/no-explicit-any */
  979. /**
  980. * Returns an {@link Analytics} instance for the given app.
  981. *
  982. * @public
  983. *
  984. * @param app - The {@link @firebase/app#FirebaseApp} to use.
  985. */
  986. function getAnalytics(app = getApp()) {
  987. app = getModularInstance(app);
  988. // Dependencies
  989. const analyticsProvider = _getProvider(app, ANALYTICS_TYPE);
  990. if (analyticsProvider.isInitialized()) {
  991. return analyticsProvider.getImmediate();
  992. }
  993. return initializeAnalytics(app);
  994. }
  995. /**
  996. * Returns an {@link Analytics} instance for the given app.
  997. *
  998. * @public
  999. *
  1000. * @param app - The {@link @firebase/app#FirebaseApp} to use.
  1001. */
  1002. function initializeAnalytics(app, options = {}) {
  1003. // Dependencies
  1004. const analyticsProvider = _getProvider(app, ANALYTICS_TYPE);
  1005. if (analyticsProvider.isInitialized()) {
  1006. const existingInstance = analyticsProvider.getImmediate();
  1007. if (deepEqual(options, analyticsProvider.getOptions())) {
  1008. return existingInstance;
  1009. }
  1010. else {
  1011. throw ERROR_FACTORY.create("already-initialized" /* AnalyticsError.ALREADY_INITIALIZED */);
  1012. }
  1013. }
  1014. const analyticsInstance = analyticsProvider.initialize({ options });
  1015. return analyticsInstance;
  1016. }
  1017. /**
  1018. * This is a public static method provided to users that wraps four different checks:
  1019. *
  1020. * 1. Check if it's not a browser extension environment.
  1021. * 2. Check if cookies are enabled in current browser.
  1022. * 3. Check if IndexedDB is supported by the browser environment.
  1023. * 4. Check if the current browser context is valid for using `IndexedDB.open()`.
  1024. *
  1025. * @public
  1026. *
  1027. */
  1028. async function isSupported() {
  1029. if (isBrowserExtension()) {
  1030. return false;
  1031. }
  1032. if (!areCookiesEnabled()) {
  1033. return false;
  1034. }
  1035. if (!isIndexedDBAvailable()) {
  1036. return false;
  1037. }
  1038. try {
  1039. const isDBOpenable = await validateIndexedDBOpenable();
  1040. return isDBOpenable;
  1041. }
  1042. catch (error) {
  1043. return false;
  1044. }
  1045. }
  1046. /**
  1047. * Use gtag `config` command to set `screen_name`.
  1048. *
  1049. * @public
  1050. *
  1051. * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
  1052. * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
  1053. *
  1054. * @param analyticsInstance - The {@link Analytics} instance.
  1055. * @param screenName - Screen name to set.
  1056. */
  1057. function setCurrentScreen(analyticsInstance, screenName, options) {
  1058. analyticsInstance = getModularInstance(analyticsInstance);
  1059. setCurrentScreen$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], screenName, options).catch(e => logger.error(e));
  1060. }
  1061. /**
  1062. * Use gtag `config` command to set `user_id`.
  1063. *
  1064. * @public
  1065. *
  1066. * @param analyticsInstance - The {@link Analytics} instance.
  1067. * @param id - User ID to set.
  1068. */
  1069. function setUserId(analyticsInstance, id, options) {
  1070. analyticsInstance = getModularInstance(analyticsInstance);
  1071. setUserId$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], id, options).catch(e => logger.error(e));
  1072. }
  1073. /**
  1074. * Use gtag `config` command to set all params specified.
  1075. *
  1076. * @public
  1077. */
  1078. function setUserProperties(analyticsInstance, properties, options) {
  1079. analyticsInstance = getModularInstance(analyticsInstance);
  1080. setUserProperties$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], properties, options).catch(e => logger.error(e));
  1081. }
  1082. /**
  1083. * Sets whether Google Analytics collection is enabled for this app on this device.
  1084. * Sets global `window['ga-disable-analyticsId'] = true;`
  1085. *
  1086. * @public
  1087. *
  1088. * @param analyticsInstance - The {@link Analytics} instance.
  1089. * @param enabled - If true, enables collection, if false, disables it.
  1090. */
  1091. function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
  1092. analyticsInstance = getModularInstance(analyticsInstance);
  1093. setAnalyticsCollectionEnabled$1(initializationPromisesMap[analyticsInstance.app.options.appId], enabled).catch(e => logger.error(e));
  1094. }
  1095. /**
  1096. * Adds data that will be set on every event logged from the SDK, including automatic ones.
  1097. * With gtag's "set" command, the values passed persist on the current page and are passed with
  1098. * all subsequent events.
  1099. * @public
  1100. * @param customParams - Any custom params the user may pass to gtag.js.
  1101. */
  1102. function setDefaultEventParameters(customParams) {
  1103. // Check if reference to existing gtag function on window object exists
  1104. if (wrappedGtagFunction) {
  1105. wrappedGtagFunction("set" /* GtagCommand.SET */, customParams);
  1106. }
  1107. else {
  1108. _setDefaultEventParametersForInit(customParams);
  1109. }
  1110. }
  1111. /**
  1112. * Sends a Google Analytics event with given `eventParams`. This method
  1113. * automatically associates this logged event with this Firebase web
  1114. * app instance on this device.
  1115. * List of official event parameters can be found in the gtag.js
  1116. * reference documentation:
  1117. * {@link https://developers.google.com/gtagjs/reference/ga4-events
  1118. * | the GA4 reference documentation}.
  1119. *
  1120. * @public
  1121. */
  1122. function logEvent(analyticsInstance, eventName, eventParams, options) {
  1123. analyticsInstance = getModularInstance(analyticsInstance);
  1124. logEvent$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], eventName, eventParams, options).catch(e => logger.error(e));
  1125. }
  1126. /**
  1127. * Sets the applicable end user consent state for this web app across all gtag references once
  1128. * Firebase Analytics is initialized.
  1129. *
  1130. * Use the {@link ConsentSettings} to specify individual consent type values. By default consent
  1131. * types are set to "granted".
  1132. * @public
  1133. * @param consentSettings - Maps the applicable end user consent state for gtag.js.
  1134. */
  1135. function setConsent(consentSettings) {
  1136. // Check if reference to existing gtag function on window object exists
  1137. if (wrappedGtagFunction) {
  1138. wrappedGtagFunction("consent" /* GtagCommand.CONSENT */, 'update', consentSettings);
  1139. }
  1140. else {
  1141. _setConsentDefaultForInit(consentSettings);
  1142. }
  1143. }
  1144. const name = "@firebase/analytics";
  1145. const version = "0.9.1";
  1146. /**
  1147. * Firebase Analytics
  1148. *
  1149. * @packageDocumentation
  1150. */
  1151. function registerAnalytics() {
  1152. _registerComponent(new Component(ANALYTICS_TYPE, (container, { options: analyticsOptions }) => {
  1153. // getImmediate for FirebaseApp will always succeed
  1154. const app = container.getProvider('app').getImmediate();
  1155. const installations = container
  1156. .getProvider('installations-internal')
  1157. .getImmediate();
  1158. return factory(app, installations, analyticsOptions);
  1159. }, "PUBLIC" /* ComponentType.PUBLIC */));
  1160. _registerComponent(new Component('analytics-internal', internalFactory, "PRIVATE" /* ComponentType.PRIVATE */));
  1161. registerVersion(name, version);
  1162. // BUILD_TARGET will be replaced by values like esm5, esm2017, cjs5, etc during the compilation
  1163. registerVersion(name, version, 'esm2017');
  1164. function internalFactory(container) {
  1165. try {
  1166. const analytics = container.getProvider(ANALYTICS_TYPE).getImmediate();
  1167. return {
  1168. logEvent: (eventName, eventParams, options) => logEvent(analytics, eventName, eventParams, options)
  1169. };
  1170. }
  1171. catch (e) {
  1172. throw ERROR_FACTORY.create("interop-component-reg-failed" /* AnalyticsError.INTEROP_COMPONENT_REG_FAILED */, {
  1173. reason: e
  1174. });
  1175. }
  1176. }
  1177. }
  1178. registerAnalytics();
  1179. export { getAnalytics, initializeAnalytics, isSupported, logEvent, setAnalyticsCollectionEnabled, setConsent, setCurrentScreen, setDefaultEventParameters, setUserId, setUserProperties, settings };
  1180. //# sourceMappingURL=index.esm2017.js.map