@firebase/analytics 0.7.11 → 0.8.0-20220707195244

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:
@@ -666,6 +672,188 @@ var AnalyticsAbortSignal = /** @class */ (function () {
666
672
  return AnalyticsAbortSignal;
667
673
  }());
668
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
+
669
857
  /**
670
858
  * @license
671
859
  * Copyright 2020 Google LLC
@@ -766,6 +954,11 @@ function _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToApp
766
954
  if (!findGtagScriptOnPage()) {
767
955
  insertScriptTag(dataLayerName, dynamicConfig.measurementId);
768
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
+ }
769
962
  // This command initializes gtag.js and only needs to be called once for the entire web app,
770
963
  // but since it is idempotent, we can call it multiple times.
771
964
  // We keep it together with other initialization logic for better code structure.
@@ -783,6 +976,11 @@ function _initializeAnalytics(app, dynamicConfigPromisesList, measurementIdToApp
783
976
  // Note: This will trigger a page_view event unless 'send_page_view' is set to false in
784
977
  // `configProperties`.
785
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
+ }
786
984
  return [2 /*return*/, dynamicConfig.measurementId];
787
985
  }
788
986
  });
@@ -947,162 +1145,6 @@ function factory(app, installations, options) {
947
1145
  return analyticsInstance;
948
1146
  }
949
1147
 
950
- /**
951
- * @license
952
- * Copyright 2019 Google LLC
953
- *
954
- * Licensed under the Apache License, Version 2.0 (the "License");
955
- * you may not use this file except in compliance with the License.
956
- * You may obtain a copy of the License at
957
- *
958
- * http://www.apache.org/licenses/LICENSE-2.0
959
- *
960
- * Unless required by applicable law or agreed to in writing, software
961
- * distributed under the License is distributed on an "AS IS" BASIS,
962
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
963
- * See the License for the specific language governing permissions and
964
- * limitations under the License.
965
- */
966
- /**
967
- * Logs an analytics event through the Firebase SDK.
968
- *
969
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
970
- * @param eventName Google Analytics event name, choose from standard list or use a custom string.
971
- * @param eventParams Analytics event parameters.
972
- */
973
- function logEvent$1(gtagFunction, initializationPromise, eventName, eventParams, options) {
974
- return tslib.__awaiter(this, void 0, void 0, function () {
975
- var measurementId, params;
976
- return tslib.__generator(this, function (_a) {
977
- switch (_a.label) {
978
- case 0:
979
- if (!(options && options.global)) return [3 /*break*/, 1];
980
- gtagFunction("event" /* EVENT */, eventName, eventParams);
981
- return [2 /*return*/];
982
- case 1: return [4 /*yield*/, initializationPromise];
983
- case 2:
984
- measurementId = _a.sent();
985
- params = tslib.__assign(tslib.__assign({}, eventParams), { 'send_to': measurementId });
986
- gtagFunction("event" /* EVENT */, eventName, params);
987
- _a.label = 3;
988
- case 3: return [2 /*return*/];
989
- }
990
- });
991
- });
992
- }
993
- /**
994
- * Set screen_name parameter for this Google Analytics ID.
995
- *
996
- * @deprecated Use {@link logEvent} with `eventName` as 'screen_view' and add relevant `eventParams`.
997
- * See {@link https://firebase.google.com/docs/analytics/screenviews | Track Screenviews}.
998
- *
999
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
1000
- * @param screenName Screen name string to set.
1001
- */
1002
- function setCurrentScreen$1(gtagFunction, initializationPromise, screenName, options) {
1003
- return tslib.__awaiter(this, void 0, void 0, function () {
1004
- var measurementId;
1005
- return tslib.__generator(this, function (_a) {
1006
- switch (_a.label) {
1007
- case 0:
1008
- if (!(options && options.global)) return [3 /*break*/, 1];
1009
- gtagFunction("set" /* SET */, { 'screen_name': screenName });
1010
- return [2 /*return*/, Promise.resolve()];
1011
- case 1: return [4 /*yield*/, initializationPromise];
1012
- case 2:
1013
- measurementId = _a.sent();
1014
- gtagFunction("config" /* CONFIG */, measurementId, {
1015
- update: true,
1016
- 'screen_name': screenName
1017
- });
1018
- _a.label = 3;
1019
- case 3: return [2 /*return*/];
1020
- }
1021
- });
1022
- });
1023
- }
1024
- /**
1025
- * Set user_id parameter for this Google Analytics ID.
1026
- *
1027
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
1028
- * @param id User ID string to set
1029
- */
1030
- function setUserId$1(gtagFunction, initializationPromise, id, options) {
1031
- return tslib.__awaiter(this, void 0, void 0, function () {
1032
- var measurementId;
1033
- return tslib.__generator(this, function (_a) {
1034
- switch (_a.label) {
1035
- case 0:
1036
- if (!(options && options.global)) return [3 /*break*/, 1];
1037
- gtagFunction("set" /* SET */, { 'user_id': id });
1038
- return [2 /*return*/, Promise.resolve()];
1039
- case 1: return [4 /*yield*/, initializationPromise];
1040
- case 2:
1041
- measurementId = _a.sent();
1042
- gtagFunction("config" /* CONFIG */, measurementId, {
1043
- update: true,
1044
- 'user_id': id
1045
- });
1046
- _a.label = 3;
1047
- case 3: return [2 /*return*/];
1048
- }
1049
- });
1050
- });
1051
- }
1052
- /**
1053
- * Set all other user properties other than user_id and screen_name.
1054
- *
1055
- * @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
1056
- * @param properties Map of user properties to set
1057
- */
1058
- function setUserProperties$1(gtagFunction, initializationPromise, properties, options) {
1059
- return tslib.__awaiter(this, void 0, void 0, function () {
1060
- var flatProperties, _i, _a, key, measurementId;
1061
- return tslib.__generator(this, function (_b) {
1062
- switch (_b.label) {
1063
- case 0:
1064
- if (!(options && options.global)) return [3 /*break*/, 1];
1065
- flatProperties = {};
1066
- for (_i = 0, _a = Object.keys(properties); _i < _a.length; _i++) {
1067
- key = _a[_i];
1068
- // use dot notation for merge behavior in gtag.js
1069
- flatProperties["user_properties." + key] = properties[key];
1070
- }
1071
- gtagFunction("set" /* SET */, flatProperties);
1072
- return [2 /*return*/, Promise.resolve()];
1073
- case 1: return [4 /*yield*/, initializationPromise];
1074
- case 2:
1075
- measurementId = _b.sent();
1076
- gtagFunction("config" /* CONFIG */, measurementId, {
1077
- update: true,
1078
- 'user_properties': properties
1079
- });
1080
- _b.label = 3;
1081
- case 3: return [2 /*return*/];
1082
- }
1083
- });
1084
- });
1085
- }
1086
- /**
1087
- * Set whether collection is enabled for this ID.
1088
- *
1089
- * @param enabled If true, collection is enabled for this ID.
1090
- */
1091
- function setAnalyticsCollectionEnabled$1(initializationPromise, enabled) {
1092
- return tslib.__awaiter(this, void 0, void 0, function () {
1093
- var measurementId;
1094
- return tslib.__generator(this, function (_a) {
1095
- switch (_a.label) {
1096
- case 0: return [4 /*yield*/, initializationPromise];
1097
- case 1:
1098
- measurementId = _a.sent();
1099
- window["ga-disable-" + measurementId] = !enabled;
1100
- return [2 /*return*/];
1101
- }
1102
- });
1103
- });
1104
- }
1105
-
1106
1148
  /* eslint-disable @typescript-eslint/no-explicit-any */
1107
1149
  /**
1108
1150
  * Returns an {@link Analytics} instance for the given app.
@@ -1234,6 +1276,22 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1234
1276
  analyticsInstance = util.getModularInstance(analyticsInstance);
1235
1277
  setAnalyticsCollectionEnabled$1(initializationPromisesMap[analyticsInstance.app.options.appId], enabled).catch(function (e) { return logger.error(e); });
1236
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
+ }
1237
1295
  /**
1238
1296
  * Sends a Google Analytics event with given `eventParams`. This method
1239
1297
  * automatically associates this logged event with this Firebase web
@@ -1248,10 +1306,28 @@ function setAnalyticsCollectionEnabled(analyticsInstance, enabled) {
1248
1306
  function logEvent(analyticsInstance, eventName, eventParams, options) {
1249
1307
  analyticsInstance = util.getModularInstance(analyticsInstance);
1250
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
+ }
1251
1327
  }
1252
1328
 
1253
1329
  var name = "@firebase/analytics";
1254
- var version = "0.7.11";
1330
+ var version = "0.8.0-20220707195244";
1255
1331
 
1256
1332
  /**
1257
1333
  * Firebase Analytics
@@ -1293,7 +1369,9 @@ exports.initializeAnalytics = initializeAnalytics;
1293
1369
  exports.isSupported = isSupported;
1294
1370
  exports.logEvent = logEvent;
1295
1371
  exports.setAnalyticsCollectionEnabled = setAnalyticsCollectionEnabled;
1372
+ exports.setConsent = setConsent;
1296
1373
  exports.setCurrentScreen = setCurrentScreen;
1374
+ exports.setDefaultEventParameters = setDefaultEventParameters;
1297
1375
  exports.setUserId = setUserId;
1298
1376
  exports.setUserProperties = setUserProperties;
1299
1377
  exports.settings = settings;