@firebase/analytics 0.7.10 → 0.7.11-canary.13550089f

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.
@@ -234,6 +234,10 @@ measurementIdToAppId) {
234
234
  // If CONFIG, second arg must be measurementId.
235
235
  await gtagOnConfig(gtagCore, initializationPromisesMap, dynamicConfigPromisesList, measurementIdToAppId, idOrNameOrParams, gtagParams);
236
236
  }
237
+ else if (command === "consent" /* CONSENT */) {
238
+ // If CONFIG, second arg must be measurementId.
239
+ gtagCore("consent" /* CONSENT */, 'update', gtagParams);
240
+ }
237
241
  else {
238
242
  // If SET, second arg must be params.
239
243
  gtagCore("set" /* SET */, idOrNameOrParams);
@@ -456,6 +460,7 @@ retryData = defaultRetryData, timeoutMillis) {
456
460
  */
457
461
  async function attemptFetchDynamicConfigWithRetry(appFields, { throttleEndTimeMillis, backoffCount }, signal, retryData = defaultRetryData // for testing
458
462
  ) {
463
+ var _a, _b;
459
464
  const { appId, measurementId } = appFields;
460
465
  // Starts with a (potentially zero) timeout to support resumption from stored state.
461
466
  // Ensures the throttle end time is honored if the last attempt timed out.
@@ -467,7 +472,7 @@ async function attemptFetchDynamicConfigWithRetry(appFields, { throttleEndTimeMi
467
472
  if (measurementId) {
468
473
  logger.warn(`Timed out fetching this Firebase app's measurement ID from the server.` +
469
474
  ` Falling back to the measurement ID ${measurementId}` +
470
- ` provided in the "measurementId" field in the local Firebase config. [${e.message}]`);
475
+ ` provided in the "measurementId" field in the local Firebase config. [${(_a = e) === null || _a === void 0 ? void 0 : _a.message}]`);
471
476
  return { appId, measurementId };
472
477
  }
473
478
  throw e;
@@ -479,19 +484,20 @@ async function attemptFetchDynamicConfigWithRetry(appFields, { throttleEndTimeMi
479
484
  return response;
480
485
  }
481
486
  catch (e) {
482
- if (!isRetriableError(e)) {
487
+ const error = e;
488
+ if (!isRetriableError(error)) {
483
489
  retryData.deleteThrottleMetadata(appId);
484
490
  if (measurementId) {
485
491
  logger.warn(`Failed to fetch this Firebase app's measurement ID from the server.` +
486
492
  ` Falling back to the measurement ID ${measurementId}` +
487
- ` provided in the "measurementId" field in the local Firebase config. [${e.message}]`);
493
+ ` provided in the "measurementId" field in the local Firebase config. [${error === null || error === void 0 ? void 0 : error.message}]`);
488
494
  return { appId, measurementId };
489
495
  }
490
496
  else {
491
497
  throw e;
492
498
  }
493
499
  }
494
- const backoffMillis = Number(e.customData.httpStatus) === 503
500
+ const backoffMillis = Number((_b = error === null || error === void 0 ? void 0 : error.customData) === null || _b === void 0 ? void 0 : _b.httpStatus) === 503
495
501
  ? calculateBackoffMillis(backoffCount, retryData.intervalMillis, LONG_RETRY_FACTOR)
496
502
  : calculateBackoffMillis(backoffCount, retryData.intervalMillis);
497
503
  // Increments backoff state.
@@ -566,6 +572,141 @@ class AnalyticsAbortSignal {
566
572
  }
567
573
  }
568
574
 
575
+ /**
576
+ * @license
577
+ * Copyright 2019 Google LLC
578
+ *
579
+ * Licensed under the Apache License, Version 2.0 (the "License");
580
+ * you may not use this file except in compliance with the License.
581
+ * You may obtain a copy of the License at
582
+ *
583
+ * http://www.apache.org/licenses/LICENSE-2.0
584
+ *
585
+ * Unless required by applicable law or agreed to in writing, software
586
+ * distributed under the License is distributed on an "AS IS" BASIS,
587
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
588
+ * See the License for the specific language governing permissions and
589
+ * limitations under the License.
590
+ */
591
+ /**
592
+ * Event parameters to set on 'gtag' during initialization.
593
+ */
594
+ let defaultEventParametersForInit;
595
+ /**
596
+ * Logs an analytics event through the Firebase SDK.
597
+ *
598
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
599
+ * @param eventName Google Analytics event name, choose from standard list or use a custom string.
600
+ * @param eventParams Analytics event parameters.
601
+ */
602
+ async function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
603
+ if (options && options.global) {
604
+ gtagFunction("event" /* EVENT */, eventName, eventParams);
605
+ return;
606
+ }
607
+ else {
608
+ const measurementId = await initializationPromise;
609
+ const params = Object.assign(Object.assign({}, eventParams), { 'send_to': measurementId });
610
+ gtagFunction("event" /* EVENT */, eventName, params);
611
+ }
612
+ }
613
+ /**
614
+ * Set screen_name parameter for this Google Analytics ID.
615
+ *
616
+ * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
617
+ * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
618
+ *
619
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
620
+ * @param screenName Screen name string to set.
621
+ */
622
+ async function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
623
+ if (options && options.global) {
624
+ gtagFunction("set" /* SET */, { 'screen_name': screenName });
625
+ return Promise.resolve();
626
+ }
627
+ else {
628
+ const measurementId = await initializationPromise;
629
+ gtagFunction("config" /* CONFIG */, measurementId, {
630
+ update: true,
631
+ 'screen_name': screenName
632
+ });
633
+ }
634
+ }
635
+ /**
636
+ * Set user_id parameter for this Google Analytics ID.
637
+ *
638
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
639
+ * @param id User ID string to set
640
+ */
641
+ async function setUserId$1(gtagFunction, initializationPromise, id, options) {
642
+ if (options && options.global) {
643
+ gtagFunction("set" /* SET */, { 'user_id': id });
644
+ return Promise.resolve();
645
+ }
646
+ else {
647
+ const measurementId = await initializationPromise;
648
+ gtagFunction("config" /* CONFIG */, measurementId, {
649
+ update: true,
650
+ 'user_id': id
651
+ });
652
+ }
653
+ }
654
+ /**
655
+ * Set all other user properties other than user_id and screen_name.
656
+ *
657
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
658
+ * @param properties Map of user properties to set
659
+ */
660
+ async function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
661
+ if (options && options.global) {
662
+ const flatProperties = {};
663
+ for (const key of Object.keys(properties)) {
664
+ // use dot notation for merge behavior in gtag.js
665
+ flatProperties[`user_properties.${key}`] = properties[key];
666
+ }
667
+ gtagFunction("set" /* SET */, flatProperties);
668
+ return Promise.resolve();
669
+ }
670
+ else {
671
+ const measurementId = await initializationPromise;
672
+ gtagFunction("config" /* CONFIG */, measurementId, {
673
+ update: true,
674
+ 'user_properties': properties
675
+ });
676
+ }
677
+ }
678
+ /**
679
+ * Set whether collection is enabled for this ID.
680
+ *
681
+ * @param enabled If true, collection is enabled for this ID.
682
+ */
683
+ async function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
684
+ const measurementId = await initializationPromise;
685
+ window[`ga-disable-${measurementId}`] = !enabled;
686
+ }
687
+ /**
688
+ * Consent parameters to default to during 'gtag' initialization.
689
+ */
690
+ let defaultConsentSettingsForInit;
691
+ /**
692
+ * Sets the variable {@link defaultConsentSettingsForInit} for use in the initialization of
693
+ * analytics.
694
+ *
695
+ * @param consentSettings Maps the applicable end user consent state for gtag.js.
696
+ */
697
+ function _setConsentDefaultForInit(consentSettings) {
698
+ defaultConsentSettingsForInit = consentSettings;
699
+ }
700
+ /**
701
+ * Sets the variable `defaultEventParametersForInit` for use in the initialization of
702
+ * analytics.
703
+ *
704
+ * @param customParams Any custom params the user may pass to gtag.js.
705
+ */
706
+ function _setDefaultEventParametersForInit(customParams) {
707
+ defaultEventParametersForInit = customParams;
708
+ }
709
+
569
710
  /**
570
711
  * @license
571
712
  * Copyright 2020 Google LLC
@@ -583,6 +724,7 @@ class AnalyticsAbortSignal {
583
724
  * limitations under the License.
584
725
  */
585
726
  async function validateIndexedDB() {
727
+ var _a;
586
728
  if (!isIndexedDBAvailable()) {
587
729
  logger.warn(ERROR_FACTORY.create("indexeddb-unavailable" /* INDEXEDDB_UNAVAILABLE */, {
588
730
  errorInfo: 'IndexedDB is not available in this environment.'
@@ -595,7 +737,7 @@ async function validateIndexedDB() {
595
737
  }
596
738
  catch (e) {
597
739
  logger.warn(ERROR_FACTORY.create("indexeddb-unavailable" /* INDEXEDDB_UNAVAILABLE */, {
598
- errorInfo: e
740
+ errorInfo: (_a = e) === null || _a === void 0 ? void 0 : _a.toString()
599
741
  }).message);
600
742
  return false;
601
743
  }
@@ -650,6 +792,11 @@ async function _initializeAnalytics(app, dynamicConfigPromisesList, measurementI
650
792
  if (!findGtagScriptOnPage()) {
651
793
  insertScriptTag(dataLayerName, dynamicConfig.measurementId);
652
794
  }
795
+ // Detects if there are consent settings that need to be configured.
796
+ if (defaultConsentSettingsForInit) {
797
+ gtagCore("consent" /* CONSENT */, 'default', defaultConsentSettingsForInit);
798
+ _setConsentDefaultForInit(undefined);
799
+ }
653
800
  // This command initializes gtag.js and only needs to be called once for the entire web app,
654
801
  // but since it is idempotent, we can call it multiple times.
655
802
  // We keep it together with other initialization logic for better code structure.
@@ -669,6 +816,11 @@ async function _initializeAnalytics(app, dynamicConfigPromisesList, measurementI
669
816
  // Note: This will trigger a page_view event unless 'send_page_view' is set to false in
670
817
  // `configProperties`.
671
818
  gtagCore("config" /* CONFIG */, dynamicConfig.measurementId, configProperties);
819
+ // Detects if there is data that will be set on every event logged from the SDK.
820
+ if (defaultEventParametersForInit) {
821
+ gtagCore("set" /* SET */, defaultEventParametersForInit);
822
+ _setDefaultEventParametersForInit(undefined);
823
+ }
672
824
  return dynamicConfig.measurementId;
673
825
  }
674
826
 
@@ -829,115 +981,6 @@ function factory(app, installations, options) {
829
981
  return analyticsInstance;
830
982
  }
831
983
 
832
- /**
833
- * @license
834
- * Copyright 2019 Google LLC
835
- *
836
- * Licensed under the Apache License, Version 2.0 (the "License");
837
- * you may not use this file except in compliance with the License.
838
- * You may obtain a copy of the License at
839
- *
840
- * http://www.apache.org/licenses/LICENSE-2.0
841
- *
842
- * Unless required by applicable law or agreed to in writing, software
843
- * distributed under the License is distributed on an "AS IS" BASIS,
844
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
845
- * See the License for the specific language governing permissions and
846
- * limitations under the License.
847
- */
848
- /**
849
- * Logs an analytics event through the Firebase SDK.
850
- *
851
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
852
- * @param eventName Google Analytics event name, choose from standard list or use a custom string.
853
- * @param eventParams Analytics event parameters.
854
- */
855
- async function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
856
- if (options && options.global) {
857
- gtagFunction("event" /* EVENT */, eventName, eventParams);
858
- return;
859
- }
860
- else {
861
- const measurementId = await initializationPromise;
862
- const params = Object.assign(Object.assign({}, eventParams), { 'send_to': measurementId });
863
- gtagFunction("event" /* EVENT */, eventName, params);
864
- }
865
- }
866
- /**
867
- * Set screen_name parameter for this Google Analytics ID.
868
- *
869
- * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
870
- * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
871
- *
872
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
873
- * @param screenName Screen name string to set.
874
- */
875
- async function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
876
- if (options && options.global) {
877
- gtagFunction("set" /* SET */, { 'screen_name': screenName });
878
- return Promise.resolve();
879
- }
880
- else {
881
- const measurementId = await initializationPromise;
882
- gtagFunction("config" /* CONFIG */, measurementId, {
883
- update: true,
884
- 'screen_name': screenName
885
- });
886
- }
887
- }
888
- /**
889
- * Set user_id parameter for this Google Analytics ID.
890
- *
891
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
892
- * @param id User ID string to set
893
- */
894
- async function setUserId$1(gtagFunction, initializationPromise, id, options) {
895
- if (options && options.global) {
896
- gtagFunction("set" /* SET */, { 'user_id': id });
897
- return Promise.resolve();
898
- }
899
- else {
900
- const measurementId = await initializationPromise;
901
- gtagFunction("config" /* CONFIG */, measurementId, {
902
- update: true,
903
- 'user_id': id
904
- });
905
- }
906
- }
907
- /**
908
- * Set all other user properties other than user_id and screen_name.
909
- *
910
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
911
- * @param properties Map of user properties to set
912
- */
913
- async function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
914
- if (options && options.global) {
915
- const flatProperties = {};
916
- for (const key of Object.keys(properties)) {
917
- // use dot notation for merge behavior in gtag.js
918
- flatProperties[`user_properties.${key}`] = properties[key];
919
- }
920
- gtagFunction("set" /* SET */, flatProperties);
921
- return Promise.resolve();
922
- }
923
- else {
924
- const measurementId = await initializationPromise;
925
- gtagFunction("config" /* CONFIG */, measurementId, {
926
- update: true,
927
- 'user_properties': properties
928
- });
929
- }
930
- }
931
- /**
932
- * Set whether collection is enabled for this ID.
933
- *
934
- * @param enabled If true, collection is enabled for this ID.
935
- */
936
- async function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
937
- const measurementId = await initializationPromise;
938
- window[`ga-disable-${measurementId}`] = !enabled;
939
- }
940
-
941
984
  /* eslint-disable @typescript-eslint/no-explicit-any */
942
985
  /**
943
986
  * Returns an {@link Analytics} instance for the given app.
@@ -1055,6 +1098,22 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1055
1098
  analyticsInstance = getModularInstance(analyticsInstance);
1056
1099
  setAnalyticsCollectionEnabled$1(initializationPromisesMap[analyticsInstance.app.options.appId], enabled).catch(e => logger.error(e));
1057
1100
  }
1101
+ /**
1102
+ * Adds data that will be set on every event logged from the SDK, including automatic ones.
1103
+ * With gtag's "set" command, the values passed persist on the current page and are passed with
1104
+ * all subsequent events.
1105
+ * @public
1106
+ * @param customParams - Any custom params the user may pass to gtag.js.
1107
+ */
1108
+ function setDefaultEventParameters(customParams) {
1109
+ // Check if reference to existing gtag function on window object exists
1110
+ if (wrappedGtagFunction) {
1111
+ wrappedGtagFunction("set" /* SET */, customParams);
1112
+ }
1113
+ else {
1114
+ _setDefaultEventParametersForInit(customParams);
1115
+ }
1116
+ }
1058
1117
  /**
1059
1118
  * Sends a Google Analytics event with given `eventParams`. This method
1060
1119
  * automatically associates this logged event with this Firebase web
@@ -1069,10 +1128,28 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1069
1128
  function logEvent(analyticsInstance, eventName, eventParams, options) {
1070
1129
  analyticsInstance = getModularInstance(analyticsInstance);
1071
1130
  logEvent$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], eventName, eventParams, options).catch(e => logger.error(e));
1131
+ }
1132
+ /**
1133
+ * Sets the applicable end user consent state for this web app across all gtag references once
1134
+ * Firebase Analytics is initialized.
1135
+ *
1136
+ * Use the {@link ConsentSettings} to specify individual consent type values. By default consent
1137
+ * types are set to "granted".
1138
+ * @public
1139
+ * @param consentSettings - Maps the applicable end user consent state for gtag.js.
1140
+ */
1141
+ function setConsent(consentSettings) {
1142
+ // Check if reference to existing gtag function on window object exists
1143
+ if (wrappedGtagFunction) {
1144
+ wrappedGtagFunction("consent" /* CONSENT */, 'update', consentSettings);
1145
+ }
1146
+ else {
1147
+ _setConsentDefaultForInit(consentSettings);
1148
+ }
1072
1149
  }
1073
1150
 
1074
1151
  const name = "@firebase/analytics";
1075
- const version = "0.7.10";
1152
+ const version = "0.7.11-canary.13550089f";
1076
1153
 
1077
1154
  /**
1078
1155
  * Firebase Analytics
@@ -1108,5 +1185,5 @@ function registerAnalytics() {
1108
1185
  }
1109
1186
  registerAnalytics();
1110
1187
 
1111
- export { getAnalytics, initializeAnalytics, isSupported, logEvent, setAnalyticsCollectionEnabled, setCurrentScreen, setUserId, setUserProperties, settings };
1188
+ export { getAnalytics, initializeAnalytics, isSupported, logEvent, setAnalyticsCollectionEnabled, setConsent, setCurrentScreen, setDefaultEventParameters, setUserId, setUserProperties, settings };
1112
1189
  //# sourceMappingURL=index.esm2017.js.map