@zerodot/google-analytics 1.0.0 → 1.1.0
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 +183 -61
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -10,12 +10,14 @@ const DATA_API = "https://analyticsdata.googleapis.com";
|
|
|
10
10
|
const REPORTING_API = "https://analyticsreporting.googleapis.com";
|
|
11
11
|
/** The Admin API. The credential check uses it. */
|
|
12
12
|
const ADMIN_API = "https://analyticsadmin.googleapis.com";
|
|
13
|
-
|
|
13
|
+
/** The Management and Metadata APIs v3 of Universal Analytics. */
|
|
14
|
+
const LEGACY_API = "https://www.googleapis.com";
|
|
15
|
+
const text$1 = (value) => typeof value === "string" && value !== "" ? value : void 0;
|
|
14
16
|
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
15
17
|
/** The access token of a credential. The server refreshes it. */
|
|
16
18
|
function analyticsToken(credential) {
|
|
17
19
|
if (credential?.type === "google_analytics.oauth2") {
|
|
18
|
-
const token = text(credential.data.accessToken);
|
|
20
|
+
const token = text$1(credential.data.accessToken);
|
|
19
21
|
if (!token) throw new Error("The Google Analytics credential is not connected to an account yet");
|
|
20
22
|
return token;
|
|
21
23
|
}
|
|
@@ -30,15 +32,16 @@ const HINTS = {
|
|
|
30
32
|
/** A clear error from a Google answer: `{ error: { code, message, status } }`. */
|
|
31
33
|
function analyticsError(status, answer, action) {
|
|
32
34
|
const error = isObject(answer) && isObject(answer.error) ? answer.error : {};
|
|
33
|
-
const message = text(error.message) ?? "no message";
|
|
34
|
-
const code = text(error.status);
|
|
35
|
+
const message = text$1(error.message) ?? "no message";
|
|
36
|
+
const code = text$1(error.status);
|
|
35
37
|
const hint = HINTS[status] ?? "";
|
|
36
38
|
return /* @__PURE__ */ new Error(`Google Analytics could not ${action} (${status}${code ? ` ${code}` : ""}): ${message}.${hint}`);
|
|
37
39
|
}
|
|
38
40
|
const BASE = {
|
|
39
41
|
data: DATA_API,
|
|
40
42
|
reporting: REPORTING_API,
|
|
41
|
-
admin: ADMIN_API
|
|
43
|
+
admin: ADMIN_API,
|
|
44
|
+
legacy: LEGACY_API
|
|
42
45
|
};
|
|
43
46
|
function urlOf(request) {
|
|
44
47
|
const url = new URL(`${BASE[request.api]}${request.path}`);
|
|
@@ -126,6 +129,9 @@ const analyticsOAuth2Credential = {
|
|
|
126
129
|
test: check
|
|
127
130
|
};
|
|
128
131
|
// src/config.ts
|
|
132
|
+
/** The ID in the URL of a GA4 property or a view in Google Analytics. */
|
|
133
|
+
const PROPERTY_URL_PATTERN = "analytics\\.google\\.com/analytics.*/p([0-9]+)";
|
|
134
|
+
const VIEW_URL_PATTERN = "analytics\\.google\\.com/analytics.*p([0-9]+)";
|
|
129
135
|
const RESOURCES = ["report", "userActivity"];
|
|
130
136
|
const OPERATIONS_OF = {
|
|
131
137
|
report: ["get"],
|
|
@@ -241,13 +247,20 @@ const filterGroup$1 = zod.z.enum(["andGroup", "orGroup"]).default("andGroup").me
|
|
|
241
247
|
orGroup: "Or Group"
|
|
242
248
|
}
|
|
243
249
|
});
|
|
244
|
-
|
|
250
|
+
/** A name field that loads its options. The user can still type any name. */
|
|
251
|
+
const loadedName = (loadOptions, dependsOn) => zod.z.string().optional().meta({
|
|
245
252
|
title: "Name",
|
|
246
|
-
description: "The API name. Used when the list name is \"other\""
|
|
253
|
+
description: "The API name. Used when the list name is \"other\"",
|
|
254
|
+
loadOptions,
|
|
255
|
+
dependsOn
|
|
247
256
|
});
|
|
257
|
+
const ga4MetricName = loadedName("ga4Metrics", ["propertyId"]);
|
|
258
|
+
const ga4DimensionName = loadedName("ga4Dimensions", ["propertyId"]);
|
|
259
|
+
const uaMetricName = loadedName("uaMetrics", ["viewId"]);
|
|
260
|
+
const uaDimensionName = loadedName("uaDimensions", ["viewId"]);
|
|
248
261
|
const ga4MetricItem = zod.z.object({
|
|
249
262
|
listName: ga4Metric,
|
|
250
|
-
name:
|
|
263
|
+
name: ga4MetricName.meta({ description: "The API name of the metric. For \"Custom metric\", any name for the column" }),
|
|
251
264
|
expression: zod.z.string().optional().meta({
|
|
252
265
|
title: "Expression",
|
|
253
266
|
description: "A mathematical expression for derived metrics. For example, the metric Event count per user is eventCount/totalUsers"
|
|
@@ -259,11 +272,11 @@ const ga4MetricItem = zod.z.object({
|
|
|
259
272
|
});
|
|
260
273
|
const ga4DimensionItem = zod.z.object({
|
|
261
274
|
listName: ga4Dimension,
|
|
262
|
-
name:
|
|
275
|
+
name: ga4DimensionName
|
|
263
276
|
});
|
|
264
277
|
const stringFilter = zod.z.object({
|
|
265
278
|
listName: ga4Dimension,
|
|
266
|
-
name:
|
|
279
|
+
name: ga4DimensionName,
|
|
267
280
|
value: zod.z.string().default(""),
|
|
268
281
|
caseSensitive: zod.z.boolean().default(true),
|
|
269
282
|
matchType: zod.z.enum([
|
|
@@ -287,27 +300,27 @@ const stringFilter = zod.z.object({
|
|
|
287
300
|
});
|
|
288
301
|
const inListFilter = zod.z.object({
|
|
289
302
|
listName: ga4Dimension,
|
|
290
|
-
name:
|
|
303
|
+
name: ga4DimensionName,
|
|
291
304
|
values: zod.z.string().default("").meta({ description: "Comma separated list of values. Must be non-empty" }),
|
|
292
305
|
caseSensitive: zod.z.boolean().default(true)
|
|
293
306
|
});
|
|
294
307
|
const dimensionNumericFilter = zod.z.object({
|
|
295
308
|
listName: ga4Dimension,
|
|
296
|
-
name:
|
|
309
|
+
name: ga4DimensionName,
|
|
297
310
|
valueType,
|
|
298
311
|
value: zod.z.string().default(""),
|
|
299
312
|
operation: numericOperation
|
|
300
313
|
});
|
|
301
314
|
const metricNumericFilter = zod.z.object({
|
|
302
315
|
listName: ga4Metric,
|
|
303
|
-
name:
|
|
316
|
+
name: ga4MetricName,
|
|
304
317
|
valueType,
|
|
305
318
|
value: zod.z.string().default(""),
|
|
306
319
|
operation: numericOperation
|
|
307
320
|
});
|
|
308
321
|
const betweenFilter = zod.z.object({
|
|
309
322
|
listName: ga4Metric,
|
|
310
|
-
name:
|
|
323
|
+
name: ga4MetricName,
|
|
311
324
|
valueType,
|
|
312
325
|
fromValue: zod.z.string().default(""),
|
|
313
326
|
toValue: zod.z.string().default("")
|
|
@@ -330,11 +343,19 @@ const metricFilters = zod.z.object({ filterExpressions: zod.z.object({
|
|
|
330
343
|
const orderBy = zod.z.object({
|
|
331
344
|
metricOrderBy: zod.z.array(zod.z.object({
|
|
332
345
|
desc: zod.z.boolean().default(false).meta({ title: "Descending" }),
|
|
333
|
-
metricName: zod.z.string().default("").meta({
|
|
346
|
+
metricName: zod.z.string().default("").meta({
|
|
347
|
+
title: "Metric Name",
|
|
348
|
+
loadOptions: "ga4Metrics",
|
|
349
|
+
dependsOn: ["propertyId"]
|
|
350
|
+
})
|
|
334
351
|
})).default([]),
|
|
335
352
|
dimmensionOrderBy: zod.z.array(zod.z.object({
|
|
336
353
|
desc: zod.z.boolean().default(false).meta({ title: "Descending" }),
|
|
337
|
-
dimensionName: zod.z.string().default("").meta({
|
|
354
|
+
dimensionName: zod.z.string().default("").meta({
|
|
355
|
+
title: "Dimension Name",
|
|
356
|
+
loadOptions: "ga4Dimensions",
|
|
357
|
+
dependsOn: ["propertyId"]
|
|
358
|
+
}),
|
|
338
359
|
orderType: zod.z.enum([
|
|
339
360
|
"ALPHANUMERIC",
|
|
340
361
|
"CASE_INSENSITIVE_ALPHANUMERIC",
|
|
@@ -384,7 +405,7 @@ const uaMetricItem = zod.z.object({
|
|
|
384
405
|
title: "Metric",
|
|
385
406
|
labels: labelsOf(UA_METRICS)
|
|
386
407
|
}),
|
|
387
|
-
name:
|
|
408
|
+
name: uaMetricName.meta({ description: "The name of the metric. For \"Custom metric\", any name for the column" }),
|
|
388
409
|
expression: zod.z.string().optional().meta({
|
|
389
410
|
title: "Expression",
|
|
390
411
|
description: "A metric expression, for example ga:totalRefunds/ga:users"
|
|
@@ -413,11 +434,11 @@ const uaDimension = zod.z.enum(valuesOf(UA_DIMENSIONS)).default("ga:date").meta(
|
|
|
413
434
|
});
|
|
414
435
|
const uaDimensionItem = zod.z.object({
|
|
415
436
|
listName: uaDimension,
|
|
416
|
-
name:
|
|
437
|
+
name: uaDimensionName
|
|
417
438
|
});
|
|
418
439
|
const uaDimensionFilter = zod.z.object({
|
|
419
440
|
listName: uaDimension,
|
|
420
|
-
name:
|
|
441
|
+
name: uaDimensionName,
|
|
421
442
|
operator: zod.z.enum([
|
|
422
443
|
"BEGINS_WITH",
|
|
423
444
|
"ENDS_WITH",
|
|
@@ -512,12 +533,16 @@ const analyticsConfig = zod.z.object({
|
|
|
512
533
|
propertyId: zod.z.string().optional().meta({
|
|
513
534
|
showWhen: GA4,
|
|
514
535
|
title: "Property",
|
|
515
|
-
description: "The ID of the property, for example 123456, or the URL of the property in Google Analytics"
|
|
536
|
+
description: "The ID of the property, for example 123456, or the URL of the property in Google Analytics",
|
|
537
|
+
loadOptions: "properties",
|
|
538
|
+
urlPattern: PROPERTY_URL_PATTERN
|
|
516
539
|
}),
|
|
517
540
|
viewId: zod.z.string().optional().meta({
|
|
518
541
|
showWhen: [UNIVERSAL, USER_ACTIVITY],
|
|
519
542
|
title: "View",
|
|
520
|
-
description: "The ID of the view, for example 123456. For a report, the URL of the view in Google Analytics also works"
|
|
543
|
+
description: "The ID of the view, for example 123456. For a report, the URL of the view in Google Analytics also works",
|
|
544
|
+
loadOptions: "views",
|
|
545
|
+
urlPattern: VIEW_URL_PATTERN
|
|
521
546
|
}),
|
|
522
547
|
userId: zod.z.string().optional().meta({
|
|
523
548
|
showWhen: USER_ACTIVITY,
|
|
@@ -597,7 +622,8 @@ const analyticsConfig = zod.z.object({
|
|
|
597
622
|
metricAggregations: []
|
|
598
623
|
}).meta({
|
|
599
624
|
showWhen: GA4,
|
|
600
|
-
title: "Additional Fields"
|
|
625
|
+
title: "Additional Fields",
|
|
626
|
+
ui: "collection"
|
|
601
627
|
}),
|
|
602
628
|
additionalFieldsUA: uaAdditionalFields.default({
|
|
603
629
|
hideTotals: false,
|
|
@@ -606,7 +632,8 @@ const analyticsConfig = zod.z.object({
|
|
|
606
632
|
useResourceQuotas: false
|
|
607
633
|
}).meta({
|
|
608
634
|
showWhen: UNIVERSAL,
|
|
609
|
-
title: "Additional Fields"
|
|
635
|
+
title: "Additional Fields",
|
|
636
|
+
ui: "collection"
|
|
610
637
|
}),
|
|
611
638
|
activityTypes: zod.z.array(zod.z.enum(ACTIVITY_TYPES)).default([]).meta({
|
|
612
639
|
showWhen: USER_ACTIVITY,
|
|
@@ -629,9 +656,9 @@ const analyticsConfig = zod.z.object({
|
|
|
629
656
|
});
|
|
630
657
|
});
|
|
631
658
|
// src/request.ts
|
|
632
|
-
const isRecord$
|
|
659
|
+
const isRecord$2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
633
660
|
const asList$1 = (value) => Array.isArray(value) ? value : [];
|
|
634
|
-
const records = (value) => asList$1(value).filter(isRecord$
|
|
661
|
+
const records$1 = (value) => asList$1(value).filter(isRecord$2);
|
|
635
662
|
const str = (value) => typeof value === "string" ? value : "";
|
|
636
663
|
/** The ID of a GA4 property, from its URL, `properties/123`, or the bare number. */
|
|
637
664
|
function parsePropertyId(input) {
|
|
@@ -755,7 +782,7 @@ function fieldOf(item, kind) {
|
|
|
755
782
|
/** The filter entries of a group, in the form of the API. */
|
|
756
783
|
function processFilters(expression, source) {
|
|
757
784
|
const out = [];
|
|
758
|
-
for (const item of records(expression.stringFilter)) out.push({ filter: {
|
|
785
|
+
for (const item of records$1(expression.stringFilter)) out.push({ filter: {
|
|
759
786
|
fieldName: fieldOf(item, source),
|
|
760
787
|
stringFilter: {
|
|
761
788
|
value: str(item.value),
|
|
@@ -763,14 +790,14 @@ function processFilters(expression, source) {
|
|
|
763
790
|
matchType: item.matchType
|
|
764
791
|
}
|
|
765
792
|
} });
|
|
766
|
-
for (const item of records(expression.inListFilter)) out.push({ filter: {
|
|
793
|
+
for (const item of records$1(expression.inListFilter)) out.push({ filter: {
|
|
767
794
|
fieldName: fieldOf(item, source),
|
|
768
795
|
inListFilter: {
|
|
769
796
|
values: str(item.values).split(",").map((value) => value.trim()).filter((value) => value !== ""),
|
|
770
797
|
caseSensitive: item.caseSensitive
|
|
771
798
|
}
|
|
772
799
|
} });
|
|
773
|
-
for (const item of records(expression.betweenFilter)) {
|
|
800
|
+
for (const item of records$1(expression.betweenFilter)) {
|
|
774
801
|
const type = str(item.valueType);
|
|
775
802
|
out.push({ filter: {
|
|
776
803
|
fieldName: fieldOf(item, source),
|
|
@@ -780,7 +807,7 @@ function processFilters(expression, source) {
|
|
|
780
807
|
}
|
|
781
808
|
} });
|
|
782
809
|
}
|
|
783
|
-
for (const item of records(expression.numericFilter)) out.push({ filter: {
|
|
810
|
+
for (const item of records$1(expression.numericFilter)) out.push({ filter: {
|
|
784
811
|
fieldName: fieldOf(item, source),
|
|
785
812
|
numericFilter: {
|
|
786
813
|
operation: item.operation,
|
|
@@ -791,14 +818,14 @@ function processFilters(expression, source) {
|
|
|
791
818
|
}
|
|
792
819
|
/** A filter group: `{ andGroup: { expressions } }`. Empty gives undefined. */
|
|
793
820
|
function filterGroup(ui, source) {
|
|
794
|
-
const group = isRecord$
|
|
795
|
-
if (!group || !isRecord$
|
|
821
|
+
const group = isRecord$2(ui) && isRecord$2(ui.filterExpressions) ? ui.filterExpressions : void 0;
|
|
822
|
+
if (!group || !isRecord$2(group.expression)) return void 0;
|
|
796
823
|
const expressions = processFilters(group.expression, source);
|
|
797
824
|
if (expressions.length === 0) return void 0;
|
|
798
825
|
return { [str(group.filterExpressionType) || "andGroup"]: { expressions } };
|
|
799
826
|
}
|
|
800
|
-
function ga4Metrics(collection) {
|
|
801
|
-
const metrics = records(isRecord$
|
|
827
|
+
function ga4Metrics$1(collection) {
|
|
828
|
+
const metrics = records$1(isRecord$2(collection) ? collection.metricValues : []).map((metric) => {
|
|
802
829
|
switch (metric.listName) {
|
|
803
830
|
case "other": return { name: nameOf(metric, "metric") };
|
|
804
831
|
case "custom": {
|
|
@@ -813,17 +840,17 @@ function ga4Metrics(collection) {
|
|
|
813
840
|
checkDuplicates(metrics, "name", "metrics");
|
|
814
841
|
return metrics;
|
|
815
842
|
}
|
|
816
|
-
function ga4Dimensions(collection) {
|
|
817
|
-
const dimensions = records(isRecord$
|
|
843
|
+
function ga4Dimensions$1(collection) {
|
|
844
|
+
const dimensions = records$1(isRecord$2(collection) ? collection.dimensionValues : []).map((dimension) => ({ name: nameOf(dimension, "dimension") }));
|
|
818
845
|
checkDuplicates(dimensions, "name", "dimensions");
|
|
819
846
|
return dimensions;
|
|
820
847
|
}
|
|
821
848
|
function orderBys(ui) {
|
|
822
|
-
if (!isRecord$
|
|
823
|
-
return [...records(ui.metricOrderBy).map((order) => ({
|
|
849
|
+
if (!isRecord$2(ui)) return [];
|
|
850
|
+
return [...records$1(ui.metricOrderBy).map((order) => ({
|
|
824
851
|
desc: order.desc,
|
|
825
852
|
metric: { metricName: order.metricName }
|
|
826
|
-
})), ...records(ui.dimmensionOrderBy).map((order) => ({
|
|
853
|
+
})), ...records$1(ui.dimmensionOrderBy).map((order) => ({
|
|
827
854
|
desc: order.desc,
|
|
828
855
|
dimension: {
|
|
829
856
|
dimensionName: order.dimensionName,
|
|
@@ -835,9 +862,9 @@ function orderBys(ui) {
|
|
|
835
862
|
function ga4Body(config, dateRanges) {
|
|
836
863
|
const fields = config.additionalFields;
|
|
837
864
|
const body = { dateRanges };
|
|
838
|
-
const metrics = ga4Metrics(config.metricsGA4);
|
|
865
|
+
const metrics = ga4Metrics$1(config.metricsGA4);
|
|
839
866
|
if (metrics.length > 0) body.metrics = metrics;
|
|
840
|
-
const dimensions = ga4Dimensions(config.dimensionsGA4);
|
|
867
|
+
const dimensions = ga4Dimensions$1(config.dimensionsGA4);
|
|
841
868
|
if (dimensions.length > 0) body.dimensions = dimensions;
|
|
842
869
|
if (fields.currencyCode) body.currencyCode = fields.currencyCode;
|
|
843
870
|
const dimensionFilter = filterGroup(fields.dimensionFiltersUI, "dimension");
|
|
@@ -860,13 +887,13 @@ const runReportRequest = (propertyId, body) => ({
|
|
|
860
887
|
});
|
|
861
888
|
/** The rows of a `runReport` answer as flat objects: dimension and metric names are the keys. */
|
|
862
889
|
function simplifyGA4(response) {
|
|
863
|
-
if (!isRecord$
|
|
864
|
-
const dimensionNames = records(response.dimensionHeaders).map((header) => str(header.name));
|
|
865
|
-
const metricNames = records(response.metricHeaders).map((header) => str(header.name));
|
|
890
|
+
if (!isRecord$2(response) || !Array.isArray(response.rows)) return [];
|
|
891
|
+
const dimensionNames = records$1(response.dimensionHeaders).map((header) => str(header.name));
|
|
892
|
+
const metricNames = records$1(response.metricHeaders).map((header) => str(header.name));
|
|
866
893
|
const out = [];
|
|
867
|
-
for (const row of records(response.rows)) {
|
|
868
|
-
const dimensionValues = records(row.dimensionValues);
|
|
869
|
-
const metricValues = records(row.metricValues);
|
|
894
|
+
for (const row of records$1(response.rows)) {
|
|
895
|
+
const dimensionValues = records$1(row.dimensionValues);
|
|
896
|
+
const metricValues = records$1(row.metricValues);
|
|
870
897
|
const item = {};
|
|
871
898
|
dimensionNames.forEach((name, index) => {
|
|
872
899
|
item[name] = dimensionValues[index]?.value;
|
|
@@ -878,8 +905,8 @@ function simplifyGA4(response) {
|
|
|
878
905
|
}
|
|
879
906
|
return out;
|
|
880
907
|
}
|
|
881
|
-
function uaMetrics(collection) {
|
|
882
|
-
const metrics = records(isRecord$
|
|
908
|
+
function uaMetrics$1(collection) {
|
|
909
|
+
const metrics = records$1(isRecord$2(collection) ? collection.metricValues : []).map((metric) => {
|
|
883
910
|
switch (metric.listName) {
|
|
884
911
|
case "other": {
|
|
885
912
|
const name = nameOf(metric, "metric");
|
|
@@ -909,12 +936,12 @@ function uaBody(config, dateRanges) {
|
|
|
909
936
|
viewId: parseViewId(config.viewId),
|
|
910
937
|
dateRanges
|
|
911
938
|
};
|
|
912
|
-
const metrics = uaMetrics(config.metricsUA);
|
|
939
|
+
const metrics = uaMetrics$1(config.metricsUA);
|
|
913
940
|
if (metrics.length > 0) body.metrics = metrics;
|
|
914
|
-
const dimensions = records(isRecord$
|
|
941
|
+
const dimensions = records$1(isRecord$2(config.dimensionsUA) ? config.dimensionsUA.dimensionValues : []).map((dimension) => ({ name: nameOf(dimension, "dimension") }));
|
|
915
942
|
checkDuplicates(dimensions, "name", "dimensions");
|
|
916
943
|
if (dimensions.length > 0) body.dimensions = dimensions;
|
|
917
|
-
const filters = records(fields.dimensionFiltersUi?.filterValues).map((filter) => ({
|
|
944
|
+
const filters = records$1(fields.dimensionFiltersUi?.filterValues).map((filter) => ({
|
|
918
945
|
dimensionName: nameOf(filter, "dimension"),
|
|
919
946
|
operator: filter.operator,
|
|
920
947
|
expressions: [filter.expressions]
|
|
@@ -936,20 +963,20 @@ const batchGetRequest = (reportRequest, useResourceQuotas) => ({
|
|
|
936
963
|
/** The rows of the reports as flat objects. A report with no rows gives nothing. */
|
|
937
964
|
function simplifyUA(reports) {
|
|
938
965
|
const out = [];
|
|
939
|
-
for (const report of records(reports)) {
|
|
940
|
-
const header = isRecord$
|
|
941
|
-
const data = isRecord$
|
|
966
|
+
for (const report of records$1(reports)) {
|
|
967
|
+
const header = isRecord$2(report.columnHeader) ? report.columnHeader : {};
|
|
968
|
+
const data = isRecord$2(report.data) ? report.data : {};
|
|
942
969
|
if (!Array.isArray(data.rows)) continue;
|
|
943
970
|
const dimensions = asList$1(header.dimensions).map(str);
|
|
944
|
-
const metricHeader = isRecord$
|
|
945
|
-
const metrics = records(metricHeader.metricHeaderEntries).map((entry) => str(entry.name));
|
|
946
|
-
for (const row of records(data.rows)) {
|
|
971
|
+
const metricHeader = isRecord$2(header.metricHeader) ? header.metricHeader : {};
|
|
972
|
+
const metrics = records$1(metricHeader.metricHeaderEntries).map((entry) => str(entry.name));
|
|
973
|
+
for (const row of records$1(data.rows)) {
|
|
947
974
|
const item = {};
|
|
948
975
|
const rowDimensions = asList$1(row.dimensions);
|
|
949
976
|
dimensions.forEach((name, index) => {
|
|
950
977
|
item[name] = rowDimensions[index];
|
|
951
978
|
});
|
|
952
|
-
const firstMetrics = records(row.metrics)[0];
|
|
979
|
+
const firstMetrics = records$1(row.metrics)[0];
|
|
953
980
|
const values = asList$1(firstMetrics?.values);
|
|
954
981
|
metrics.forEach((name, index) => {
|
|
955
982
|
item[name] = values[index];
|
|
@@ -961,11 +988,11 @@ function simplifyUA(reports) {
|
|
|
961
988
|
}
|
|
962
989
|
/** One report with the rows of all the reports. */
|
|
963
990
|
function mergeUA(reports) {
|
|
964
|
-
const list = records(reports);
|
|
991
|
+
const list = records$1(reports);
|
|
965
992
|
const first = list[0];
|
|
966
993
|
if (!first) return [];
|
|
967
|
-
const data = isRecord$
|
|
968
|
-
const rows = list.flatMap((report) => isRecord$
|
|
994
|
+
const data = isRecord$2(first.data) ? first.data : {};
|
|
995
|
+
const rows = list.flatMap((report) => isRecord$2(report.data) ? asList$1(report.data.rows) : []);
|
|
969
996
|
return [{
|
|
970
997
|
columnHeader: first.columnHeader,
|
|
971
998
|
data: {
|
|
@@ -991,6 +1018,93 @@ const userActivityRequest = (body) => ({
|
|
|
991
1018
|
body,
|
|
992
1019
|
action: "search the user activity"
|
|
993
1020
|
});
|
|
1021
|
+
// src/loaders.ts
|
|
1022
|
+
const LOAD_TIMEOUT_MS = 2e4;
|
|
1023
|
+
/** The most pages of one list, so a loop never runs for ever. */
|
|
1024
|
+
const MAX_PAGES$1 = 50;
|
|
1025
|
+
const isRecord$1 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1026
|
+
const records = (value) => Array.isArray(value) ? value.filter(isRecord$1) : [];
|
|
1027
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
1028
|
+
const get = async (context, api, path, query = {}) => {
|
|
1029
|
+
const answer = await callAnalytics(analyticsToken(context.credential), {
|
|
1030
|
+
api,
|
|
1031
|
+
method: "GET",
|
|
1032
|
+
path,
|
|
1033
|
+
query,
|
|
1034
|
+
action: "load the options"
|
|
1035
|
+
}, {
|
|
1036
|
+
signal: context.signal,
|
|
1037
|
+
timeoutMs: LOAD_TIMEOUT_MS
|
|
1038
|
+
});
|
|
1039
|
+
return isRecord$1(answer) ? answer : {};
|
|
1040
|
+
};
|
|
1041
|
+
/** Sort by label, as the lists of the app do. Not by case. */
|
|
1042
|
+
const sortOptions = (options) => [...options].sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase()));
|
|
1043
|
+
/** The GA4 properties of all accounts: the Admin API account summaries. */
|
|
1044
|
+
async function properties(context) {
|
|
1045
|
+
const options = [];
|
|
1046
|
+
let pageToken;
|
|
1047
|
+
for (let page = 0; page < MAX_PAGES$1; page++) {
|
|
1048
|
+
const answer = await get(context, "admin", "/v1beta/accountSummaries", {
|
|
1049
|
+
pageSize: "200",
|
|
1050
|
+
pageToken
|
|
1051
|
+
});
|
|
1052
|
+
for (const account of records(answer.accountSummaries)) for (const property of records(account.propertySummaries)) {
|
|
1053
|
+
const name = text(property.property);
|
|
1054
|
+
const value = name.split("/")[1] || name;
|
|
1055
|
+
if (!value) continue;
|
|
1056
|
+
options.push({
|
|
1057
|
+
label: text(property.displayName) || value,
|
|
1058
|
+
value,
|
|
1059
|
+
description: text(account.displayName) || void 0
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
pageToken = text(answer.nextPageToken) || void 0;
|
|
1063
|
+
if (!pageToken) break;
|
|
1064
|
+
}
|
|
1065
|
+
return sortOptions(options);
|
|
1066
|
+
}
|
|
1067
|
+
/** The Universal Analytics views of all accounts. */
|
|
1068
|
+
async function views(context) {
|
|
1069
|
+
const answer = await get(context, "legacy", "/analytics/v3/management/accounts/~all/webproperties/~all/profiles");
|
|
1070
|
+
return sortOptions(records(answer.items).map((item) => ({
|
|
1071
|
+
label: text(item.name),
|
|
1072
|
+
value: text(item.id),
|
|
1073
|
+
description: text(item.websiteUrl) || void 0
|
|
1074
|
+
})));
|
|
1075
|
+
}
|
|
1076
|
+
/** The property of the editor. The loader does not run while it is empty. */
|
|
1077
|
+
const propertyOf = (context) => {
|
|
1078
|
+
const value = context.config.propertyId;
|
|
1079
|
+
return parsePropertyId(typeof value === "string" ? value : void 0);
|
|
1080
|
+
};
|
|
1081
|
+
async function ga4Metadata(context, field) {
|
|
1082
|
+
const answer = await get(context, "data", `/v1beta/properties/${encodeURIComponent(propertyOf(context))}/metadata`, { fields: field });
|
|
1083
|
+
return sortOptions(records(answer[field]).map((item) => ({
|
|
1084
|
+
label: text(item.uiName),
|
|
1085
|
+
value: text(item.apiName),
|
|
1086
|
+
description: text(item.description) || void 0
|
|
1087
|
+
})));
|
|
1088
|
+
}
|
|
1089
|
+
const ga4Metrics = (context) => ga4Metadata(context, "metrics");
|
|
1090
|
+
const ga4Dimensions = (context) => ga4Metadata(context, "dimensions");
|
|
1091
|
+
/** The columns of Universal Analytics. A deprecated column is left out. */
|
|
1092
|
+
async function uaColumns(context, type) {
|
|
1093
|
+
const answer = await get(context, "legacy", "/analytics/v3/metadata/ga/columns");
|
|
1094
|
+
const options = [];
|
|
1095
|
+
for (const item of records(answer.items)) {
|
|
1096
|
+
const attributes = isRecord$1(item.attributes) ? item.attributes : {};
|
|
1097
|
+
if (attributes.type !== type || attributes.status === "DEPRECATED") continue;
|
|
1098
|
+
options.push({
|
|
1099
|
+
label: text(attributes.uiName),
|
|
1100
|
+
value: text(item.id),
|
|
1101
|
+
description: text(attributes.description) || void 0
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
return sortOptions(options);
|
|
1105
|
+
}
|
|
1106
|
+
const uaMetrics = (context) => uaColumns(context, "METRIC");
|
|
1107
|
+
const uaDimensions = (context) => uaColumns(context, "DIMENSION");
|
|
994
1108
|
// src/node.ts
|
|
995
1109
|
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
996
1110
|
const asList = (value) => Array.isArray(value) ? value : [];
|
|
@@ -1113,6 +1227,14 @@ const nodes = [(0, _zerodot_node_sdk.defineNode)({
|
|
|
1113
1227
|
icon: "Analytics01Icon",
|
|
1114
1228
|
iconUrl: ANALYTICS_ICON,
|
|
1115
1229
|
config: analyticsConfig,
|
|
1230
|
+
loadOptions: {
|
|
1231
|
+
properties,
|
|
1232
|
+
views,
|
|
1233
|
+
ga4Metrics,
|
|
1234
|
+
ga4Dimensions,
|
|
1235
|
+
uaMetrics,
|
|
1236
|
+
uaDimensions
|
|
1237
|
+
},
|
|
1116
1238
|
defaultConfig: {
|
|
1117
1239
|
resource: "report",
|
|
1118
1240
|
operation: "get",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zerodot/google-analytics",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Get reports and user activity from Google Analytics. Connect with Google sign-in.",
|
|
5
5
|
"license": "AGPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"google-analytics.svg"
|
|
22
22
|
],
|
|
23
23
|
"zerodot": {
|
|
24
|
-
"sdk": "^1.
|
|
24
|
+
"sdk": "^1.7",
|
|
25
25
|
"label": "Google Analytics",
|
|
26
26
|
"summary": "Get reports and user activity"
|
|
27
27
|
},
|
|
@@ -30,12 +30,12 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@zerodot/node-sdk": "^1.
|
|
33
|
+
"@zerodot/node-sdk": "^1.7.0",
|
|
34
34
|
"zod": "^4.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "26.6.5",
|
|
38
|
-
"@zerodot/node-sdk": "^1.
|
|
38
|
+
"@zerodot/node-sdk": "^1.7.0",
|
|
39
39
|
"tsdown": "0.23.2",
|
|
40
40
|
"tsx": "4.23.15",
|
|
41
41
|
"typescript": "7.0.2",
|