ga4-export-fixer 0.4.6 → 0.4.7-dev.1

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.
@@ -0,0 +1,185 @@
1
+ const { isDataformTableReferenceObject } = require('../../utils.js');
2
+ const { validateBaseConfig } = require('../../inputValidation.js');
3
+
4
+ /**
5
+ * Validates a GA4 export fixer configuration object.
6
+ * Validation is performed on mergedConfig (default values merged with user input).
7
+ * All fields are required in the merged config; optional fields are only optional for user input
8
+ * and receive their values from the default configuration during merge.
9
+ *
10
+ * @param {Object} config - The merged configuration object to validate.
11
+ * @throws {Error} If any configuration value is invalid or missing.
12
+ */
13
+ const validateEnhancedEventsConfig = (config, options = {}) => {
14
+ try {
15
+ if (!config || typeof config !== 'object' || Array.isArray(config)) {
16
+ throw new Error(`config must be a non-null object. Received: ${JSON.stringify(config)}`);
17
+ }
18
+
19
+ // base config fields (self, incremental, test, testConfig, preOperations)
20
+ validateBaseConfig(config, options);
21
+
22
+ /*
23
+ Rest of the validations are related to ga4_events_enhanced table specific fields
24
+ */
25
+
26
+ // sourceTable - required; string or Dataform table reference
27
+ if (config.sourceTable === undefined || config.sourceTable === null) {
28
+ throw new Error("config.sourceTable is required. Provide a Dataform table reference (using the ref() function) or a string in format '`project.dataset.table`'.");
29
+ }
30
+ if (isDataformTableReferenceObject(config.sourceTable)) {
31
+ // Valid Dataform reference
32
+ } else if (typeof config.sourceTable === 'string') {
33
+ if (!config.sourceTable.trim()) {
34
+ throw new Error("config.sourceTable must be a non-empty string. Received empty string.");
35
+ }
36
+ if (!/^`[^\.]+\.[^\.]+\.[^\.]+`$/.test(config.sourceTable.trim())) {
37
+ throw new Error(`config.sourceTable must be in the format '\`project.dataset.table\`' (with backticks). Received: ${JSON.stringify(config.sourceTable)}`);
38
+ }
39
+ } else {
40
+ throw new Error(`config.sourceTable must be a Dataform table reference object or a string in format '\`project.dataset.table\`'. Received: ${JSON.stringify(config.sourceTable)}`);
41
+ }
42
+
43
+ // schemaLock - optional; must be undefined or a string in "YYYYMMDD" format (e.g., "20260101")
44
+ if (typeof config.schemaLock !== 'undefined') {
45
+ if (typeof config.schemaLock !== 'string' || !/^\d{8}$/.test(config.schemaLock)) {
46
+ throw new Error(`config.schemaLock must be a string in "YYYYMMDD" format (e.g., "20260101"). Received: ${JSON.stringify(config.schemaLock)}`);
47
+ }
48
+ // Must be a valid date
49
+ const year = parseInt(config.schemaLock.slice(0, 4), 10);
50
+ const month = parseInt(config.schemaLock.slice(4, 6), 10);
51
+ const day = parseInt(config.schemaLock.slice(6, 8), 10);
52
+ const date = new Date(year, month - 1, day);
53
+ if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
54
+ throw new Error(`config.schemaLock must be a valid date. Received: ${JSON.stringify(config.schemaLock)}`);
55
+ }
56
+ // Must be at least 20241009
57
+ if (config.schemaLock < "20241009") {
58
+ throw new Error(`config.schemaLock must be a date string equal to or greater than "20241009". Received: ${JSON.stringify(config.schemaLock)}`);
59
+ }
60
+ }
61
+
62
+ // includedExportTypes - required
63
+ if (typeof config.includedExportTypes === 'undefined') {
64
+ throw new Error("config.includedExportTypes is required.");
65
+ }
66
+ if (!config.includedExportTypes || typeof config.includedExportTypes !== 'object' || Array.isArray(config.includedExportTypes)) {
67
+ throw new Error(`config.includedExportTypes must be an object. Received: ${JSON.stringify(config.includedExportTypes)}`);
68
+ }
69
+ for (const key of ['daily', 'fresh', 'intraday']) {
70
+ if (!(key in config.includedExportTypes)) {
71
+ throw new Error(`config.includedExportTypes.${key} is required.`);
72
+ }
73
+ if (typeof config.includedExportTypes[key] !== 'boolean') {
74
+ throw new Error(`config.includedExportTypes.${key} must be a boolean. Received: ${JSON.stringify(config.includedExportTypes[key])}`);
75
+ }
76
+ }
77
+ if (!config.includedExportTypes.daily && !config.includedExportTypes.fresh && !config.includedExportTypes.intraday) {
78
+ throw new Error("At least one of config.includedExportTypes.daily, config.includedExportTypes.fresh, or config.includedExportTypes.intraday must be true.");
79
+ }
80
+
81
+ // timezone - required
82
+ if (typeof config.timezone === 'undefined') {
83
+ throw new Error("config.timezone is required.");
84
+ }
85
+ if (typeof config.timezone !== 'string' || !config.timezone.trim()) {
86
+ throw new Error(`config.timezone must be a non-empty string (e.g. 'Etc/UTC', 'Europe/Helsinki'). Received: ${JSON.stringify(config.timezone)}`);
87
+ }
88
+
89
+ // customTimestampParam - optional; must be undefined or a non-empty string
90
+ if (typeof config.customTimestampParam !== 'undefined') {
91
+ if (typeof config.customTimestampParam !== 'string' || !config.customTimestampParam.trim()) {
92
+ throw new Error(`config.customTimestampParam must be a non-empty string when provided. Received: ${JSON.stringify(config.customTimestampParam)}`);
93
+ }
94
+ }
95
+
96
+ // dataIsFinal - required
97
+ if (typeof config.dataIsFinal === 'undefined') {
98
+ throw new Error("config.dataIsFinal is required.");
99
+ }
100
+ if (typeof config.dataIsFinal !== 'object' || Array.isArray(config.dataIsFinal)) {
101
+ throw new Error(`config.dataIsFinal must be an object. Received: ${JSON.stringify(config.dataIsFinal)}`);
102
+ }
103
+ if (typeof config.dataIsFinal.detectionMethod === 'undefined') {
104
+ throw new Error("config.dataIsFinal.detectionMethod is required.");
105
+ }
106
+ if (typeof config.dataIsFinal.detectionMethod !== 'string' || (config.dataIsFinal.detectionMethod !== 'EXPORT_TYPE' && config.dataIsFinal.detectionMethod !== 'DAY_THRESHOLD')) {
107
+ throw new Error(`config.dataIsFinal.detectionMethod must be 'EXPORT_TYPE' or 'DAY_THRESHOLD'. Received: ${JSON.stringify(config.dataIsFinal.detectionMethod)}`);
108
+ }
109
+ if (
110
+ config.dataIsFinal.detectionMethod === 'DAY_THRESHOLD' &&
111
+ typeof config.dataIsFinal.dayThreshold === 'undefined'
112
+ ) {
113
+ throw new Error("config.dataIsFinal.dayThreshold is required when detectionMethod is 'DAY_THRESHOLD'.");
114
+ }
115
+ if (
116
+ config.dataIsFinal.detectionMethod === 'DAY_THRESHOLD' &&
117
+ (typeof config.dataIsFinal.dayThreshold !== 'number' || !Number.isInteger(config.dataIsFinal.dayThreshold) || config.dataIsFinal.dayThreshold < 0)
118
+ ) {
119
+ throw new Error(`config.dataIsFinal.dayThreshold must be a non-negative integer. Received: ${JSON.stringify(config.dataIsFinal.dayThreshold)}`);
120
+ }
121
+ // EXPORT_TYPE detection relies on daily export tables to mark data as final.
122
+ // When daily is not enabled, all data would be marked as not final under EXPORT_TYPE,
123
+ // so DAY_THRESHOLD must be used instead.
124
+ if (
125
+ !config.includedExportTypes.daily &&
126
+ config.dataIsFinal.detectionMethod !== 'DAY_THRESHOLD'
127
+ ) {
128
+ throw new Error(`config.dataIsFinal.detectionMethod must be 'DAY_THRESHOLD' when daily export is not enabled (config.includedExportTypes.daily is false). A dayThreshold of 1 is recommended for intraday only setups. With fresh export, the GA4 data is subject to possible changes for up to 72 hours. Received: ${JSON.stringify(config.dataIsFinal.detectionMethod)}`);
129
+ }
130
+
131
+ // bufferDays - required
132
+ if (typeof config.bufferDays !== 'number' || !Number.isInteger(config.bufferDays) || config.bufferDays < 0) {
133
+ throw new Error(`config.bufferDays must be a non-negative integer. Received: ${JSON.stringify(config.bufferDays)}`);
134
+ }
135
+
136
+ // Array fields - all required
137
+ const stringArrayKeys = ['defaultExcludedEventParams', 'excludedEventParams', 'sessionParams', 'defaultExcludedEvents', 'excludedEvents', 'excludedColumns'];
138
+ for (const key of stringArrayKeys) {
139
+ if (config[key] === undefined) {
140
+ throw new Error(`config.${key} is required.`);
141
+ }
142
+ if (!Array.isArray(config[key])) {
143
+ throw new Error(`config.${key} must be an array. Received: ${JSON.stringify(config[key])}`);
144
+ }
145
+ for (let i = 0; i < config[key].length; i++) {
146
+ if (typeof config[key][i] !== 'string' || !config[key][i].trim()) {
147
+ throw new Error(`config.${key}[${i}] must be a non-empty string. Received: ${JSON.stringify(config[key][i])}`);
148
+ }
149
+ }
150
+ }
151
+
152
+ // eventParamsToColumns - required
153
+ if (config.eventParamsToColumns === undefined) {
154
+ throw new Error("config.eventParamsToColumns is required.");
155
+ }
156
+ if (!Array.isArray(config.eventParamsToColumns)) {
157
+ throw new Error(`config.eventParamsToColumns must be an array. Received: ${JSON.stringify(config.eventParamsToColumns)}`);
158
+ }
159
+ const validEventParamTypes = ['string', 'int', 'int64', 'double', 'float', 'float64'];
160
+ for (let i = 0; i < config.eventParamsToColumns.length; i++) {
161
+ const item = config.eventParamsToColumns[i];
162
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
163
+ throw new Error(`config.eventParamsToColumns[${i}] must be an object with 'name' and 'type' properties. Received: ${JSON.stringify(item)}`);
164
+ }
165
+ if (!item.name || typeof item.name !== 'string' || !item.name.trim()) {
166
+ throw new Error(`config.eventParamsToColumns[${i}].name must be a non-empty string. Received: ${JSON.stringify(item.name)}`);
167
+ }
168
+ if (item.type !== undefined && item.type !== null) {
169
+ if (!validEventParamTypes.includes(item.type)) {
170
+ throw new Error(`config.eventParamsToColumns[${i}].type must be one of: ${validEventParamTypes.join(', ')}. Received: ${JSON.stringify(item.type)}`);
171
+ }
172
+ }
173
+ if (item.columnName !== undefined && item.columnName !== null && item.columnName !== '') {
174
+ if (typeof item.columnName !== 'string' || !item.columnName.trim()) {
175
+ throw new Error(`config.eventParamsToColumns[${i}].columnName must be a non-empty string when provided. Received: ${JSON.stringify(item.columnName)}`);
176
+ }
177
+ }
178
+ }
179
+ } catch (e) {
180
+ e.message = `Config validation: ${e.message}`;
181
+ throw e;
182
+ }
183
+ };
184
+
185
+ module.exports = { validateEnhancedEventsConfig };
package/utils.js CHANGED
@@ -415,6 +415,26 @@ const processDate = (dateInput) => {
415
415
  throw new Error(`processDate: Unsupported date input format: ${JSON.stringify(dateInput)}. Expected formats are: YYYYMMDD, YYYY-MM-DD, or BigQuery SQL statement.`);
416
416
  };
417
417
 
418
+ /**
419
+ * Extracts the dataset name from a sourceTable configuration value.
420
+ *
421
+ * Supports both Dataform table reference objects (with 'dataset' or 'schema' property)
422
+ * and backtick-quoted strings in the format '`project.dataset.table`'.
423
+ *
424
+ * @param {Object|string} sourceTable - A Dataform table reference object or a backtick-quoted string.
425
+ * @returns {string} The dataset name.
426
+ * @throws {Error} If the dataset name cannot be extracted from the provided value.
427
+ */
428
+ const getDatasetName = (sourceTable) => {
429
+ if (isDataformTableReferenceObject(sourceTable)) {
430
+ return sourceTable.dataset || sourceTable.schema;
431
+ }
432
+ if (typeof sourceTable === 'string' && /^`[^\.]+\.[^\.]+\.[^\.]+`$/.test(sourceTable)) {
433
+ return sourceTable.split('.')[1];
434
+ }
435
+ throw new Error(`Unable to extract the dataset name from sourceTable, received: ${JSON.stringify(sourceTable)}`);
436
+ };
437
+
418
438
  module.exports = {
419
439
  mergeUniqueArrays,
420
440
  mergeSQLConfigurations,
@@ -423,5 +443,6 @@ module.exports = {
423
443
  isDataformTableReferenceObject,
424
444
  setDataformContext,
425
445
  selectOtherColumns,
426
- processDate
446
+ processDate,
447
+ getDatasetName
427
448
  };