-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathuseMakeRequest.ts
More file actions
205 lines (195 loc) · 5.44 KB
/
Copy pathuseMakeRequest.ts
File metadata and controls
205 lines (195 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { useMemo, useState, useCallback } from "react"
import { useSelector } from "react-redux"
import { Requestable, RequestStatus } from "@/types"
import { usePersistantObject } from "@/hooks"
import { StorageKey } from "@/constants"
import { GA4Dimensions, GA4Metrics } from "@/components/GA4Pickers"
import { FilterExpression } from "../Filter"
import { DateRange } from "../DateRanges"
import { MetricAggregation } from "../MetricAggregations"
import { PropertySummary } from "@/types/ga4/StreamPicker"
type RunReportRequest = gapi.client.analyticsdata.RunReportRequest
type OrderBy = gapi.client.analyticsdata.OrderBy
type CohortSpec = gapi.client.analyticsdata.CohortSpec
export type RunReportResponse = gapi.client.analyticsdata.RunReportResponse
type UseMakeRequestArgs = {
property: PropertySummary | undefined
dimensionFilter: FilterExpression | undefined
metricFilter: FilterExpression | undefined
dateRanges: DateRange[] | undefined
dimensions: GA4Dimensions
metrics: GA4Metrics
offset: string | undefined
limit: string | undefined
orderBys: OrderBy[] | undefined
cohortSpec: CohortSpec | undefined
metricAggregations: MetricAggregation[] | undefined
keepEmptyRows: boolean
showAdvanced: boolean
}
type Common = {
makeRequest: () => void
validRequest: boolean
request: RunReportRequest | undefined
response: RunReportResponse | undefined
}
export type RunReportError = {
code: number
message: string
status: string
}
const useMakeRequest = ({
property,
dateRanges,
dimensions,
metrics,
dimensionFilter,
metricFilter,
offset,
limit,
orderBys,
cohortSpec,
metricAggregations,
keepEmptyRows,
showAdvanced,
}: UseMakeRequestArgs): Requestable<
{ response: RunReportResponse } & Common,
Common,
Common,
{ error: RunReportError } & Common
> => {
const gapi = useSelector((state: AppState) => state.gapi)
const dataAPI = useMemo(() => gapi?.client?.analyticsdata, [gapi])
const [requestStatus, setRequestStatus] = useState(RequestStatus.NotStarted)
const [response, setResponse] = usePersistantObject<object>(
StorageKey.ga4RequestComposerBasicResponse
)
const [error, setError] = useState<any>()
const request = useMemo<RunReportRequest | undefined>(() => {
if (
(dimensions === undefined || dimensions.length === 0) &&
(metrics === undefined || metrics.length === 0)
) {
return undefined
}
const r: RunReportRequest = {}
if (dimensions !== undefined) {
r.dimensions = dimensions.map(dimension => ({
name: dimension.apiName!,
}))
}
if (metrics !== undefined) {
r.metrics = metrics.map(metric => ({ name: metric.apiName }))
}
if (dateRanges !== undefined) {
r.dateRanges = dateRanges.map(({ from, to, name }) => {
const newRange = { startDate: from, endDate: to, name }
if (name === "" || name === undefined) {
delete newRange.name
}
return newRange
})
}
if (dimensionFilter !== undefined) {
if (showAdvanced || (!showAdvanced && dimensionFilter.filter?.fieldName))
r.dimensionFilter = dimensionFilter
}
if (metricFilter !== undefined) {
if (showAdvanced || (!showAdvanced && metricFilter.filter?.fieldName))
r.metricFilter = metricFilter
}
if (offset !== undefined && offset !== "") {
r.offset = offset
}
if (limit !== undefined && limit !== "") {
r.limit = limit
}
if (orderBys !== undefined && orderBys.length !== 0) {
r.orderBys = orderBys
}
if (
showAdvanced &&
cohortSpec !== undefined &&
(cohortSpec.cohorts?.length || 0) > 0
) {
r.cohortSpec = cohortSpec
}
if (keepEmptyRows === true) {
r.keepEmptyRows = keepEmptyRows
}
if (metricAggregations !== undefined && metricAggregations.length !== 0) {
r.metricAggregations = metricAggregations as any
}
return r
}, [
dateRanges,
dimensions,
metricAggregations,
metrics,
dimensionFilter,
metricFilter,
offset,
limit,
orderBys,
cohortSpec,
keepEmptyRows,
showAdvanced,
])
const validRequest = useMemo(() => {
return request !== undefined
}, [request])
const makeRequest = useCallback(() => {
if (
dataAPI === undefined ||
request === undefined ||
property === undefined
) {
return
}
setRequestStatus(RequestStatus.InProgress)
dataAPI.properties
.runReport({ property: property.property!, resource: request })
.then(response => {
const result = response.result
setResponse(result)
setRequestStatus(RequestStatus.Successful)
})
.catch(e => {
console.error({ e })
setError(e.result.error)
setRequestStatus(RequestStatus.Failed)
})
}, [property, dataAPI, request, setResponse])
if (requestStatus === RequestStatus.Successful) {
if (response === undefined) {
throw new Error(
"Invalid invariant. Response should always be defined here."
)
}
return {
status: requestStatus,
makeRequest,
validRequest,
request,
response,
}
}
if (requestStatus === RequestStatus.Failed) {
return {
status: requestStatus,
makeRequest,
validRequest,
request,
error,
response: undefined,
}
}
return {
status: requestStatus,
makeRequest,
validRequest,
request,
response: undefined,
}
}
export default useMakeRequest