@firebase/analytics 0.7.10-canary.efe2000fc → 0.7.11-canary.1261d8323

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
@@ -281,8 +281,14 @@ measurementIdToAppId) {
281
281
  _a.sent();
282
282
  return [3 /*break*/, 5];
283
283
  case 4:
284
- // If SET, second arg must be params.
285
- gtagCore("set" /* SET */, idOrNameOrParams);
284
+ if (command === "consent" /* CONSENT */) {
285
+ // If CONFIG, second arg must be measurementId.
286
+ gtagCore("consent" /* CONSENT */, 'update', gtagParams);
287
+ }
288
+ else {
289
+ // If SET, second arg must be params.
290
+ gtagCore("set" /* SET */, idOrNameOrParams);
291
+ }
286
292
  _a.label = 5;
287
293
  case 5: return [3 /*break*/, 7];
288
294
  case 6:
@@ -540,53 +546,55 @@ retryData, timeoutMillis) {
540
546
  */
541
547
  function attemptFetchDynamicConfigWithRetry(appFields, _a, signal, retryData // for testing
542
548
  ) {
549
+ var _b, _c;
543
550
  var throttleEndTimeMillis = _a.throttleEndTimeMillis, backoffCount = _a.backoffCount;
544
551
  if (retryData === void 0) { retryData = defaultRetryData; }
545
552
  return tslib.__awaiter(this, void 0, void 0, function () {
546
- var appId, measurementId, e_1, response, e_2, backoffMillis, throttleMetadata;
547
- return tslib.__generator(this, function (_b) {
548
- switch (_b.label) {
553
+ var appId, measurementId, e_1, response, e_2, error, backoffMillis, throttleMetadata;
554
+ return tslib.__generator(this, function (_d) {
555
+ switch (_d.label) {
549
556
  case 0:
550
557
  appId = appFields.appId, measurementId = appFields.measurementId;
551
- _b.label = 1;
558
+ _d.label = 1;
552
559
  case 1:
553
- _b.trys.push([1, 3, , 4]);
560
+ _d.trys.push([1, 3, , 4]);
554
561
  return [4 /*yield*/, setAbortableTimeout(signal, throttleEndTimeMillis)];
555
562
  case 2:
556
- _b.sent();
563
+ _d.sent();
557
564
  return [3 /*break*/, 4];
558
565
  case 3:
559
- e_1 = _b.sent();
566
+ e_1 = _d.sent();
560
567
  if (measurementId) {
561
568
  logger.warn("Timed out fetching this Firebase app's measurement ID from the server." +
562
569
  (" Falling back to the measurement ID " + measurementId) +
563
- (" provided in the \"measurementId\" field in the local Firebase config. [" + e_1.message + "]"));
570
+ (" provided in the \"measurementId\" field in the local Firebase config. [" + ((_b = e_1) === null || _b === void 0 ? void 0 : _b.message) + "]"));
564
571
  return [2 /*return*/, { appId: appId, measurementId: measurementId }];
565
572
  }
566
573
  throw e_1;
567
574
  case 4:
568
- _b.trys.push([4, 6, , 7]);
575
+ _d.trys.push([4, 6, , 7]);
569
576
  return [4 /*yield*/, fetchDynamicConfig(appFields)];
570
577
  case 5:
571
- response = _b.sent();
578
+ response = _d.sent();
572
579
  // Note the SDK only clears throttle state if response is success or non-retriable.
573
580
  retryData.deleteThrottleMetadata(appId);
574
581
  return [2 /*return*/, response];
575
582
  case 6:
576
- e_2 = _b.sent();
577
- if (!isRetriableError(e_2)) {
583
+ e_2 = _d.sent();
584
+ error = e_2;
585
+ if (!isRetriableError(error)) {
578
586
  retryData.deleteThrottleMetadata(appId);
579
587
  if (measurementId) {
580
588
  logger.warn("Failed to fetch this Firebase app's measurement ID from the server." +
581
589
  (" Falling back to the measurement ID " + measurementId) +
582
- (" provided in the \"measurementId\" field in the local Firebase config. [" + e_2.message + "]"));
590
+ (" provided in the \"measurementId\" field in the local Firebase config. [" + (error === null || error === void 0 ? void 0 : error.message) + "]"));
583
591
  return [2 /*return*/, { appId: appId, measurementId: measurementId }];
584
592
  }
585
593
  else {
586
594
  throw e_2;
587
595
  }
588
596
  }
589
- backoffMillis = Number(e_2.customData.httpStatus) === 503
597
+ backoffMillis = Number((_c = error === null || error === void 0 ? void 0 : error.customData) === null || _c === void 0 ? void 0 : _c.httpStatus) === 503
590
598
  ? util.calculateBackoffMillis(backoffCount, retryData.intervalMillis, LONG_RETRY_FACTOR)
591
599
  : util.calculateBackoffMillis(backoffCount, retryData.intervalMillis);
592
600
  throttleMetadata = {
@@ -664,6 +672,188 @@ var AnalyticsAbortSignal = /** @class */ (function () {
664
672
  return AnalyticsAbortSignal;
665
673
  }());
666
674
 
675
+ /**
676
+ * @license
677
+ * Copyright 2019 Google LLC
678
+ *
679
+ * Licensed under the Apache License, Version 2.0 (the "License");
680
+ * you may not use this file except in compliance with the License.
681
+ * You may obtain a copy of the License at
682
+ *
683
+ * http://www.apache.org/licenses/LICENSE-2.0
684
+ *
685
+ * Unless required by applicable law or agreed to in writing, software
686
+ * distributed under the License is distributed on an "AS IS" BASIS,
687
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
688
+ * See the License for the specific language governing permissions and
689
+ * limitations under the License.
690
+ */
691
+ /**
692
+ * Event parameters to set on 'gtag' during initialization.
693
+ */
694
+ var defaultEventParametersForInit;
695
+ /**
696
+ * Logs an analytics event through the Firebase SDK.
697
+ *
698
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
699
+ * @param eventName Google Analytics event name, choose from standard list or use a custom string.
700
+ * @param eventParams Analytics event parameters.
701
+ */
702
+ function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
703
+ return tslib.__awaiter(this, void 0, void 0, function () {
704
+ var measurementId, params;
705
+ return tslib.__generator(this, function (_a) {
706
+ switch (_a.label) {
707
+ case 0:
708
+ if (!(options && options.global)) return [3 /*break*/, 1];
709
+ gtagFunction("event" /* EVENT */, eventName, eventParams);
710
+ return [2 /*return*/];
711
+ case 1: return [4 /*yield*/, initializationPromise];
712
+ case 2:
713
+ measurementId = _a.sent();
714
+ params = tslib.__assign(tslib.__assign({}, eventParams), { 'send_to': measurementId });
715
+ gtagFunction("event" /* EVENT */, eventName, params);
716
+ _a.label = 3;
717
+ case 3: return [2 /*return*/];
718
+ }
719
+ });
720
+ });
721
+ }
722
+ /**
723
+ * Set screen_name parameter for this Google Analytics ID.
724
+ *
725
+ * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
726
+ * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
727
+ *
728
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
729
+ * @param screenName Screen name string to set.
730
+ */
731
+ function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
732
+ return tslib.__awaiter(this, void 0, void 0, function () {
733
+ var measurementId;
734
+ return tslib.__generator(this, function (_a) {
735
+ switch (_a.label) {
736
+ case 0:
737
+ if (!(options && options.global)) return [3 /*break*/, 1];
738
+ gtagFunction("set" /* SET */, { 'screen_name': screenName });
739
+ return [2 /*return*/, Promise.resolve()];
740
+ case 1: return [4 /*yield*/, initializationPromise];
741
+ case 2:
742
+ measurementId = _a.sent();
743
+ gtagFunction("config" /* CONFIG */, measurementId, {
744
+ update: true,
745
+ 'screen_name': screenName
746
+ });
747
+ _a.label = 3;
748
+ case 3: return [2 /*return*/];
749
+ }
750
+ });
751
+ });
752
+ }
753
+ /**
754
+ * Set user_id parameter for this Google Analytics ID.
755
+ *
756
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
757
+ * @param id User ID string to set
758
+ */
759
+ function setUserId$1(gtagFunction, initializationPromise, id, options) {
760
+ return tslib.__awaiter(this, void 0, void 0, function () {
761
+ var measurementId;
762
+ return tslib.__generator(this, function (_a) {
763
+ switch (_a.label) {
764
+ case 0:
765
+ if (!(options && options.global)) return [3 /*break*/, 1];
766
+ gtagFunction("set" /* SET */, { 'user_id': id });
767
+ return [2 /*return*/, Promise.resolve()];
768
+ case 1: return [4 /*yield*/, initializationPromise];
769
+ case 2:
770
+ measurementId = _a.sent();
771
+ gtagFunction("config" /* CONFIG */, measurementId, {
772
+ update: true,
773
+ 'user_id': id
774
+ });
775
+ _a.label = 3;
776
+ case 3: return [2 /*return*/];
777
+ }
778
+ });
779
+ });
780
+ }
781
+ /**
782
+ * Set all other user properties other than user_id and screen_name.
783
+ *
784
+ * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
785
+ * @param properties Map of user properties to set
786
+ */
787
+ function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
788
+ return tslib.__awaiter(this, void 0, void 0, function () {
789
+ var flatProperties, _i, _a, key, measurementId;
790
+ return tslib.__generator(this, function (_b) {
791
+ switch (_b.label) {
792
+ case 0:
793
+ if (!(options && options.global)) return [3 /*break*/, 1];
794
+ flatProperties = {};
795
+ for (_i = 0, _a = Object.keys(properties); _i < _a.length; _i++) {
796
+ key = _a[_i];
797
+ // use dot notation for merge behavior in gtag.js
798
+ flatProperties["user_properties." + key] = properties[key];
799
+ }
800
+ gtagFunction("set" /* SET */, flatProperties);
801
+ return [2 /*return*/, Promise.resolve()];
802
+ case 1: return [4 /*yield*/, initializationPromise];
803
+ case 2:
804
+ measurementId = _b.sent();
805
+ gtagFunction("config" /* CONFIG */, measurementId, {
806
+ update: true,
807
+ 'user_properties': properties
808
+ });
809
+ _b.label = 3;
810
+ case 3: return [2 /*return*/];
811
+ }
812
+ });
813
+ });
814
+ }
815
+ /**
816
+ * Set whether collection is enabled for this ID.
817
+ *
818
+ * @param enabled If true, collection is enabled for this ID.
819
+ */
820
+ function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
821
+ return tslib.__awaiter(this, void 0, void 0, function () {
822
+ var measurementId;
823
+ return tslib.__generator(this, function (_a) {
824
+ switch (_a.label) {
825
+ case 0: return [4 /*yield*/, initializationPromise];
826
+ case 1:
827
+ measurementId = _a.sent();
828
+ window["ga-disable-" + measurementId] = !enabled;
829
+ return [2 /*return*/];
830
+ }
831
+ });
832
+ });
833
+ }
834
+ /**
835
+ * Consent parameters to default to during 'gtag' initialization.
836
+ */
837
+ var defaultConsentSettingsForInit;
838
+ /**
839
+ * Sets the variable {@link defaultConsentSettingsForInit} for use in the initialization of
840
+ * analytics.
841
+ *
842
+ * @param consentSettings Maps the applicable end user consent state for gtag.js.
843
+ */
844
+ function _setConsentDefaultForInit(consentSettings) {
845
+ defaultConsentSettingsForInit = consentSettings;
846
+ }
847
+ /**
848
+ * Sets the variable `defaultEventParametersForInit` for use in the initialization of
849
+ * analytics.
850
+ *
851
+ * @param customParams Any custom params the user may pass to gtag.js.
852
+ */
853
+ function _setDefaultEventParametersForInit(customParams) {
854
+ defaultEventParametersForInit = customParams;
855
+ }
856
+
667
857
  /**
668
858
  * @license
669
859
  * Copyright 2020 Google LLC
@@ -764,6 +954,11 @@ function _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToApp
764
954
  if (!findGtagScriptOnPage()) {
765
955
  insertScriptTag(dataLayerName, dynamicConfig.measurementId);
766
956
  }
957
+ // Detects if there are consent settings that need to be configured.
958
+ if (defaultConsentSettingsForInit) {
959
+ gtagCore("consent" /* CONSENT */, 'default', defaultConsentSettingsForInit);
960
+ _setConsentDefaultForInit(undefined);
961
+ }
767
962
  // This command initializes gtag.js and only needs to be called once for the entire web app,
768
963
  // but since it is idempotent, we can call it multiple times.
769
964
  // We keep it together with other initialization logic for better code structure.
@@ -781,6 +976,11 @@ function _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToApp
781
976
  // Note: This will trigger a page_view event unless 'send_page_view' is set to false in
782
977
  // `configProperties`.
783
978
  gtagCore("config" /* CONFIG */, dynamicConfig.measurementId, configProperties);
979
+ // Detects if there is data that will be set on every event logged from the SDK.
980
+ if (defaultEventParametersForInit) {
981
+ gtagCore("set" /* SET */, defaultEventParametersForInit);
982
+ _setDefaultEventParametersForInit(undefined);
983
+ }
784
984
  return [2 /*return*/, dynamicConfig.measurementId];
785
985
  }
786
986
  });
@@ -945,162 +1145,6 @@ function factory(app, installations, options) {
945
1145
  return analyticsInstance;
946
1146
  }
947
1147
 
948
- /**
949
- * @license
950
- * Copyright 2019 Google LLC
951
- *
952
- * Licensed under the Apache License, Version 2.0 (the "License");
953
- * you may not use this file except in compliance with the License.
954
- * You may obtain a copy of the License at
955
- *
956
- * http://www.apache.org/licenses/LICENSE-2.0
957
- *
958
- * Unless required by applicable law or agreed to in writing, software
959
- * distributed under the License is distributed on an "AS IS" BASIS,
960
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
961
- * See the License for the specific language governing permissions and
962
- * limitations under the License.
963
- */
964
- /**
965
- * Logs an analytics event through the Firebase SDK.
966
- *
967
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
968
- * @param eventName Google Analytics event name, choose from standard list or use a custom string.
969
- * @param eventParams Analytics event parameters.
970
- */
971
- function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
972
- return tslib.__awaiter(this, void 0, void 0, function () {
973
- var measurementId, params;
974
- return tslib.__generator(this, function (_a) {
975
- switch (_a.label) {
976
- case 0:
977
- if (!(options && options.global)) return [3 /*break*/, 1];
978
- gtagFunction("event" /* EVENT */, eventName, eventParams);
979
- return [2 /*return*/];
980
- case 1: return [4 /*yield*/, initializationPromise];
981
- case 2:
982
- measurementId = _a.sent();
983
- params = tslib.__assign(tslib.__assign({}, eventParams), { 'send_to': measurementId });
984
- gtagFunction("event" /* EVENT */, eventName, params);
985
- _a.label = 3;
986
- case 3: return [2 /*return*/];
987
- }
988
- });
989
- });
990
- }
991
- /**
992
- * Set screen_name parameter for this Google Analytics ID.
993
- *
994
- * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
995
- * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
996
- *
997
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
998
- * @param screenName Screen name string to set.
999
- */
1000
- function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
1001
- return tslib.__awaiter(this, void 0, void 0, function () {
1002
- var measurementId;
1003
- return tslib.__generator(this, function (_a) {
1004
- switch (_a.label) {
1005
- case 0:
1006
- if (!(options && options.global)) return [3 /*break*/, 1];
1007
- gtagFunction("set" /* SET */, { 'screen_name': screenName });
1008
- return [2 /*return*/, Promise.resolve()];
1009
- case 1: return [4 /*yield*/, initializationPromise];
1010
- case 2:
1011
- measurementId = _a.sent();
1012
- gtagFunction("config" /* CONFIG */, measurementId, {
1013
- update: true,
1014
- 'screen_name': screenName
1015
- });
1016
- _a.label = 3;
1017
- case 3: return [2 /*return*/];
1018
- }
1019
- });
1020
- });
1021
- }
1022
- /**
1023
- * Set user_id parameter for this Google Analytics ID.
1024
- *
1025
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
1026
- * @param id User ID string to set
1027
- */
1028
- function setUserId$1(gtagFunction, initializationPromise, id, options) {
1029
- return tslib.__awaiter(this, void 0, void 0, function () {
1030
- var measurementId;
1031
- return tslib.__generator(this, function (_a) {
1032
- switch (_a.label) {
1033
- case 0:
1034
- if (!(options && options.global)) return [3 /*break*/, 1];
1035
- gtagFunction("set" /* SET */, { 'user_id': id });
1036
- return [2 /*return*/, Promise.resolve()];
1037
- case 1: return [4 /*yield*/, initializationPromise];
1038
- case 2:
1039
- measurementId = _a.sent();
1040
- gtagFunction("config" /* CONFIG */, measurementId, {
1041
- update: true,
1042
- 'user_id': id
1043
- });
1044
- _a.label = 3;
1045
- case 3: return [2 /*return*/];
1046
- }
1047
- });
1048
- });
1049
- }
1050
- /**
1051
- * Set all other user properties other than user_id and screen_name.
1052
- *
1053
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
1054
- * @param properties Map of user properties to set
1055
- */
1056
- function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
1057
- return tslib.__awaiter(this, void 0, void 0, function () {
1058
- var flatProperties, _i, _a, key, measurementId;
1059
- return tslib.__generator(this, function (_b) {
1060
- switch (_b.label) {
1061
- case 0:
1062
- if (!(options && options.global)) return [3 /*break*/, 1];
1063
- flatProperties = {};
1064
- for (_i = 0, _a = Object.keys(properties); _i < _a.length; _i++) {
1065
- key = _a[_i];
1066
- // use dot notation for merge behavior in gtag.js
1067
- flatProperties["user_properties." + key] = properties[key];
1068
- }
1069
- gtagFunction("set" /* SET */, flatProperties);
1070
- return [2 /*return*/, Promise.resolve()];
1071
- case 1: return [4 /*yield*/, initializationPromise];
1072
- case 2:
1073
- measurementId = _b.sent();
1074
- gtagFunction("config" /* CONFIG */, measurementId, {
1075
- update: true,
1076
- 'user_properties': properties
1077
- });
1078
- _b.label = 3;
1079
- case 3: return [2 /*return*/];
1080
- }
1081
- });
1082
- });
1083
- }
1084
- /**
1085
- * Set whether collection is enabled for this ID.
1086
- *
1087
- * @param enabled If true, collection is enabled for this ID.
1088
- */
1089
- function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
1090
- return tslib.__awaiter(this, void 0, void 0, function () {
1091
- var measurementId;
1092
- return tslib.__generator(this, function (_a) {
1093
- switch (_a.label) {
1094
- case 0: return [4 /*yield*/, initializationPromise];
1095
- case 1:
1096
- measurementId = _a.sent();
1097
- window["ga-disable-" + measurementId] = !enabled;
1098
- return [2 /*return*/];
1099
- }
1100
- });
1101
- });
1102
- }
1103
-
1104
1148
  /* eslint-disable @typescript-eslint/no-explicit-any */
1105
1149
  /**
1106
1150
  * Returns an {@link Analytics} instance for the given app.
@@ -1232,6 +1276,22 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1232
1276
  analyticsInstance = util.getModularInstance(analyticsInstance);
1233
1277
  setAnalyticsCollectionEnabled$1(initializationPromisesMap[analyticsInstance.app.options.appId], enabled).catch(function (e) { return logger.error(e); });
1234
1278
  }
1279
+ /**
1280
+ * Adds data that will be set on every event logged from the SDK, including automatic ones.
1281
+ * With gtag's "set" command, the values passed persist on the current page and are passed with
1282
+ * all subsequent events.
1283
+ * @public
1284
+ * @param customParams - Any custom params the user may pass to gtag.js.
1285
+ */
1286
+ function setDefaultEventParameters(customParams) {
1287
+ // Check if reference to existing gtag function on window object exists
1288
+ if (wrappedGtagFunction) {
1289
+ wrappedGtagFunction("set" /* SET */, customParams);
1290
+ }
1291
+ else {
1292
+ _setDefaultEventParametersForInit(customParams);
1293
+ }
1294
+ }
1235
1295
  /**
1236
1296
  * Sends a Google Analytics event with given `eventParams`. This method
1237
1297
  * automatically associates this logged event with this Firebase web
@@ -1246,10 +1306,28 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1246
1306
  function logEvent(analyticsInstance, eventName, eventParams, options) {
1247
1307
  analyticsInstance = util.getModularInstance(analyticsInstance);
1248
1308
  logEvent$1(wrappedGtagFunction, initializationPromisesMap[analyticsInstance.app.options.appId], eventName, eventParams, options).catch(function (e) { return logger.error(e); });
1309
+ }
1310
+ /**
1311
+ * Sets the applicable end user consent state for this web app across all gtag references once
1312
+ * Firebase Analytics is initialized.
1313
+ *
1314
+ * Use the {@link ConsentSettings} to specify individual consent type values. By default consent
1315
+ * types are set to "granted".
1316
+ * @public
1317
+ * @param consentSettings - Maps the applicable end user consent state for gtag.js.
1318
+ */
1319
+ function setConsent(consentSettings) {
1320
+ // Check if reference to existing gtag function on window object exists
1321
+ if (wrappedGtagFunction) {
1322
+ wrappedGtagFunction("consent" /* CONSENT */, 'update', consentSettings);
1323
+ }
1324
+ else {
1325
+ _setConsentDefaultForInit(consentSettings);
1326
+ }
1249
1327
  }
1250
1328
 
1251
1329
  var name = "@firebase/analytics";
1252
- var version = "0.7.10-canary.efe2000fc";
1330
+ var version = "0.7.11-canary.1261d8323";
1253
1331
 
1254
1332
  /**
1255
1333
  * Firebase Analytics
@@ -1291,7 +1369,9 @@ exports.initializeAnalytics = initializeAnalytics;
1291
1369
  exports.isSupported = isSupported;
1292
1370
  exports.logEvent = logEvent;
1293
1371
  exports.setAnalyticsCollectionEnabled = setAnalyticsCollectionEnabled;
1372
+ exports.setConsent = setConsent;
1294
1373
  exports.setCurrentScreen = setCurrentScreen;
1374
+ exports.setDefaultEventParameters = setDefaultEventParameters;
1295
1375
  exports.setUserId = setUserId;
1296
1376
  exports.setUserProperties = setUserProperties;
1297
1377
  exports.settings = settings;