Skip to content

Commit f701a13

Browse files
committed
Add google provider working functions
1 parent 69973b9 commit f701a13

File tree

13 files changed

+432
-48
lines changed

13 files changed

+432
-48
lines changed

src/components/Aggregates/AggregateDataWidget.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,19 @@ const AggregateDataWidget: React.FC<Props> = ({ options, metricsMap }) => {
9292
) : (
9393
<ul style={{ margin: "0", listStyle: "none", padding: "0" }}>
9494
{data.map((item, index) => {
95+
const value =
96+
typeof item.value === "string"
97+
? Math.floor(parseInt(item.value))
98+
: Math.floor(item.value);
9599
return (
96100
<li
97101
key={index}
98102
style={{ display: "flex", justifyContent: "space-between" }}
99103
>
100-
<div style={{ fontWeight: "700" }}>{item.label}</div>
101-
<div>{item.value}</div>
104+
<div style={{ fontWeight: "700" }}>
105+
{metricsMap ? metricsMap[item.label].label : item.label}
106+
</div>
107+
<div>{value}</div>
102108
</li>
103109
);
104110
})}
@@ -117,6 +123,7 @@ export const getAggregateDataWidget = (
117123
const combinedProps: Props = {
118124
...props,
119125
options,
126+
metricsMap,
120127
};
121128
return <AggregateDataWidget {...combinedProps} />;
122129
};

src/components/Charts/PageViewsChart.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ const PageViewsChart: React.FC<Props> = ({ options, metricsMap }) => {
9797
() => [
9898
{
9999
getValue: (datum) => {
100-
return datum.value;
100+
return Math.floor(datum.value);
101101
},
102102
elementType: "line",
103+
shouldNice: true,
103104
},
104105
],
105106
[]
@@ -148,6 +149,7 @@ export const getPageViewsChart = (
148149
const combinedProps: Props = {
149150
...props,
150151
options,
152+
metricsMap,
151153
};
152154
return <PageViewsChart {...combinedProps} />;
153155
};

src/providers/google/client.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ import type { Metrics, Properties } from "../../types/widgets";
33
import { BetaAnalyticsDataClient } from "@google-analytics/data";
44
import { MetricMap, PropertyMap } from "./utilities";
55

6-
type ClientOptions = {
7-
endpoint: string;
8-
timeframe?: string;
9-
metrics?: Metrics[];
10-
property?: Properties;
11-
};
6+
type ClientOptions = {};
127

13-
function client(provider: GoogleProvider, options: ClientOptions) {
8+
function client(provider: GoogleProvider, options?: ClientOptions) {
149
const analyticsDataClient = new BetaAnalyticsDataClient({
1510
keyFilename: provider.credentials,
1611
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { GoogleProvider } from "../../types/providers";
2+
import type { GlobalAggregateOptions } from "..";
3+
import type { AggregateData } from "../../types/data";
4+
import { getMetrics, getDateRange } from "./utilities";
5+
import client from "./client";
6+
import type { protos } from "@google-analytics/data";
7+
import type { Timeframes } from "../../types/widgets";
8+
9+
async function getGlobalAggregateData(
10+
provider: GoogleProvider,
11+
options: GlobalAggregateOptions
12+
) {
13+
const googleClient = client(provider);
14+
15+
const { metrics } = options;
16+
const timeframe: Timeframes = (options.timeframe as Timeframes) ?? "30d";
17+
18+
const usedMetrics = getMetrics(metrics);
19+
20+
const dateRange = getDateRange(timeframe);
21+
22+
const request: protos.google.analytics.data.v1beta.IRunReportRequest = {
23+
property: `properties/${provider.propertyId}`,
24+
dateRanges: [dateRange.formatted],
25+
dimensions: [],
26+
metrics: usedMetrics.map((metric) => {
27+
return {
28+
name: metric,
29+
};
30+
}),
31+
keepEmptyRows: false,
32+
metricAggregations: [1],
33+
};
34+
35+
const data = await googleClient.run.runReport(request).then((data) => data);
36+
37+
const processedData: AggregateData = usedMetrics.map((metric, index) => {
38+
return {
39+
label: metrics[index],
40+
value: data[0].totals?.[0].metricValues?.[index].value ?? 0,
41+
};
42+
});
43+
44+
return processedData;
45+
}
46+
47+
export default getGlobalAggregateData;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import type { GoogleProvider } from "../../types/providers";
2+
import type { GlobalChartOptions } from "..";
3+
import type { ChartData } from "../../types/data";
4+
import {
5+
getMetrics,
6+
getDateRange,
7+
DateFormat,
8+
GoogleDateFormat,
9+
} from "./utilities";
10+
import client from "./client";
11+
import type { protos } from "@google-analytics/data";
12+
import type { Timeframes } from "../../types/widgets";
13+
import { eachDayOfInterval, parse, isEqual, format } from "date-fns";
14+
15+
async function getGlobalChartData(
16+
provider: GoogleProvider,
17+
options: GlobalChartOptions
18+
) {
19+
const googleClient = client(provider);
20+
21+
const { metrics } = options;
22+
const timeframe: Timeframes = (options.timeframe as Timeframes) ?? "30d";
23+
24+
const usedMetrics = getMetrics(metrics);
25+
26+
const dateRange = getDateRange(timeframe);
27+
28+
const dates = eachDayOfInterval({
29+
start: dateRange.dates.startDate,
30+
end: dateRange.dates.endDate,
31+
});
32+
33+
const request: protos.google.analytics.data.v1beta.IRunReportRequest = {
34+
property: `properties/${provider.propertyId}`,
35+
dateRanges: [dateRange.formatted],
36+
dimensions: [{ name: "date" }],
37+
metrics: usedMetrics.map((metric) => {
38+
return {
39+
name: metric,
40+
};
41+
}),
42+
keepEmptyRows: false,
43+
metricAggregations: [],
44+
};
45+
46+
const data = await googleClient.run.runReport(request).then((data) => data);
47+
48+
const processedData: ChartData = usedMetrics.map((metric, index) => {
49+
return {
50+
label: metrics[index],
51+
data: dates.map((date, dateIndex) => {
52+
const matchingRow = data[0].rows?.find((row) => {
53+
if (row.dimensionValues?.[0].value) {
54+
const parsedDate = parse(
55+
row.dimensionValues[0].value,
56+
GoogleDateFormat,
57+
new Date()
58+
);
59+
60+
return isEqual(date, parsedDate);
61+
}
62+
63+
return false;
64+
});
65+
66+
if (matchingRow) {
67+
const value = matchingRow.metricValues?.[index]?.value;
68+
69+
return {
70+
timestamp: format(date, DateFormat),
71+
value: value ? parseInt(value) : 0,
72+
};
73+
} else {
74+
return {
75+
timestamp: format(date, DateFormat),
76+
value: 0,
77+
};
78+
}
79+
}),
80+
};
81+
});
82+
83+
return processedData;
84+
}
85+
86+
export default getGlobalChartData;

src/providers/google/getLiveData.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import type { LiveData } from "../../types/data";
44
import client from "./client";
55

66
async function getLiveData(provider: GoogleProvider, options: LiveDataOptions) {
7-
const googleClient = client(provider, {
8-
endpoint: "/stats/realtime/visitors",
9-
});
7+
const googleClient = client(provider);
108

119
const request = {
1210
property: `properties/${provider.propertyId}`,
@@ -22,9 +20,7 @@ async function getLiveData(provider: GoogleProvider, options: LiveDataOptions) {
2220

2321
const data = await googleClient.run
2422
.runRealtimeReport(request)
25-
.then((data) => data[0].rows?.[0].metricValues?.[0]?.value);
26-
27-
console.log("data rows", data);
23+
.then((data) => data[0].rows?.[0]?.metricValues?.[0]?.value ?? null);
2824

2925
const processedData: LiveData = {
3026
visitors: data ? parseInt(data) : 0,

src/providers/google/getPageAggregateData.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,61 @@
11
import type { GoogleProvider } from "../../types/providers";
22
import type { PageAggregateOptions } from "..";
33
import type { AggregateData } from "../../types/data";
4-
import { getMetrics } from "./utilities";
4+
import { getMetrics, getDateRange } from "./utilities";
55
import client from "./client";
6+
import type { protos } from "@google-analytics/data";
7+
import type { Timeframes } from "../../types/widgets";
68

79
async function getPageAggregateData(
810
provider: GoogleProvider,
911
options: PageAggregateOptions
1012
) {
11-
const googleClient = client(provider, {
12-
endpoint: "/stats/realtime/visitors",
13-
});
13+
const googleClient = client(provider);
1414

15-
const { metrics } = options;
15+
const { metrics, pageId } = options;
16+
const timeframe: Timeframes = (options.timeframe as Timeframes) ?? "30d";
1617

1718
const usedMetrics = getMetrics(metrics);
1819

19-
console.log("usedMetrics", usedMetrics);
20+
const dateRange = getDateRange(timeframe);
2021

21-
const request = {
22+
const request: protos.google.analytics.data.v1beta.IRunReportRequest = {
2223
property: `properties/${provider.propertyId}`,
23-
dateRanges: [
24-
{
25-
startDate: "2023-03-05",
26-
endDate: "today",
27-
},
28-
],
24+
dateRanges: [dateRange.formatted],
2925
dimensions: [{ name: "pagePath" }],
3026
metrics: usedMetrics.map((metric) => {
3127
return {
3228
name: metric,
3329
};
3430
}),
31+
keepEmptyRows: false,
32+
metricAggregations: [1],
33+
dimensionFilter: {
34+
andGroup: {
35+
expressions: [
36+
{
37+
filter: {
38+
fieldName: "pagePath",
39+
stringFilter: {
40+
matchType: "EXACT",
41+
value: pageId,
42+
caseSensitive: true,
43+
},
44+
},
45+
},
46+
],
47+
},
48+
},
3549
};
3650

3751
const data = await googleClient.run.runReport(request).then((data) => data);
3852

39-
console.log("data rows", data);
40-
41-
const processedData: AggregateData = {
42-
visitors: 0,
43-
};
53+
const processedData: AggregateData = usedMetrics.map((metric, index) => {
54+
return {
55+
label: metrics[index],
56+
value: data[0].totals?.[0].metricValues?.[index].value ?? 0,
57+
};
58+
});
4459

4560
return processedData;
4661
}

0 commit comments

Comments
 (0)