Skip to content

Commit 5bea4ea

Browse files
authored
Merge pull request #307 from AmbireTech/advanced-stats
Advanced stats
2 parents 18ff65b + c5b2791 commit 5bea4ea

File tree

9 files changed

+315
-80
lines changed

9 files changed

+315
-80
lines changed

src/components/AdminPanel/SSPsAnalytics.tsx

+60-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import {
1010
NumberFormatter,
1111
MultiSelect,
1212
Fieldset,
13-
Divider
13+
Divider,
14+
ThemeIcon,
15+
Center
1416
} from '@mantine/core'
1517
import { SSPs, RequestStatPlacement, SSPsAnalyticsDataQuery } from 'types'
1618
import useSSPsAnalytics from 'hooks/useCampaignAnalytics/useSSPsAnalytics'
@@ -22,6 +24,8 @@ import { IabTaxonomyV3 } from 'adex-common'
2224
import { CATEGORIES, CAT_GROUPS, COUNTRIES, REGION_GROUPS } from 'constants/createCampaign'
2325
import MultiSelectAndRadioButtons from 'components/CreateCampaign/StepTwo/MultiSelectAndRadioButtons'
2426
import useCreateCampaignContext from 'hooks/useCreateCampaignContext'
27+
import GlobeIcon from 'resources/icons/Globe'
28+
import InvoiceIcon from 'resources/icons/Invoice'
2529

2630
const sspsData: Array<{ value: SSPs | ''; label: string }> = [
2731
{ value: '', label: 'All SSPs' },
@@ -31,11 +35,11 @@ const sspsData: Array<{ value: SSPs | ''; label: string }> = [
3135
]
3236

3337
const placementsData: Array<{ value: string; label: string }> = [
34-
{ value: '', label: 'All placements' },
3538
{ value: RequestStatPlacement.app.toString(), label: 'App' },
3639
{ value: RequestStatPlacement.siteMobile.toString(), label: 'Site - Mobile' },
3740
{ value: RequestStatPlacement.siteDesktop.toString(), label: 'Site - Desktop' },
38-
{ value: RequestStatPlacement.other.toString(), label: 'Site - other' }
41+
{ value: RequestStatPlacement.siteOther.toString(), label: 'Site - other' },
42+
{ value: RequestStatPlacement.other.toString(), label: 'Other' }
3943
]
4044

4145
const groupByData: Array<{ value: string; label: string }> = [
@@ -83,11 +87,13 @@ const mapSegmentLabel = (
8387
const SSPsAnalytics = ({
8488
country,
8589
category,
86-
format
90+
format,
91+
placement
8792
}: {
8893
category?: SSPsAnalyticsDataQuery['category']
8994
country?: SSPsAnalyticsDataQuery['country']
90-
format?: string[]
95+
format?: SSPsAnalyticsDataQuery['format']
96+
placement?: SSPsAnalyticsDataQuery['placement']
9197
}) => {
9298
// TODO: get all formats once then use it for src
9399
// NOTE: temp
@@ -101,8 +107,10 @@ const SSPsAnalytics = ({
101107
>()
102108

103109
const [ssp, setSsp] = useState<SSPs>('')
104-
const [groupBy, setGrop] = useState<SSPsAnalyticsDataQuery['groupBy']>('country')
105-
const [placement, setPlacement] = useState<RequestStatPlacement | ''>('')
110+
const [groupBy, setGrop] = useState<SSPsAnalyticsDataQuery['groupBy']>('ssp')
111+
const [selectedPlacement, setPlacement] = useState<SSPsAnalyticsDataQuery['placement']>(
112+
placement || { values: [RequestStatPlacement.siteDesktop], operator: 'in' }
113+
)
106114
const { analyticsData, getAnalyticsKeyAndUpdate } = useSSPsAnalytics()
107115
const [selectedCountries, setCountries] = useState<SSPsAnalyticsDataQuery['country']>(
108116
country || { values: [], operator: 'in' }
@@ -111,7 +119,7 @@ const SSPsAnalytics = ({
111119
category || { values: [], operator: 'in' }
112120
)
113121

114-
const [selectedFormats, setFormats] = useState<string[]>(format || [])
122+
const [selectedFormats, setFormats] = useState<SSPsAnalyticsDataQuery['format']>(format || [])
115123

116124
const analytics = useMemo(
117125
() => analyticsData.get(analyticsKey?.key || ''),
@@ -125,11 +133,12 @@ const SSPsAnalytics = ({
125133
const key = await getAnalyticsKeyAndUpdate({
126134
...removeOptionalEmptyStringProps({
127135
ssp,
128-
placement,
136+
placement: selectedPlacement,
129137
category: selectedCategories,
130138
country: selectedCountries,
131139
format: selectedFormats
132140
}),
141+
limit: 666,
133142
groupBy
134143
})
135144
setAnalyticsKey(key)
@@ -147,6 +156,7 @@ const SSPsAnalytics = ({
147156
selectedCategories,
148157
selectedCountries,
149158
selectedFormats,
159+
selectedPlacement,
150160
ssp
151161
])
152162

@@ -208,11 +218,17 @@ const SSPsAnalytics = ({
208218
searchable
209219
size="sm"
210220
/>
211-
<Select
221+
<MultiSelect
212222
label="Placement"
213-
value={placement?.toString()}
223+
value={selectedPlacement?.values?.map((x) => x.toString())}
214224
// @ts-ignore
215-
onChange={(val) => setPlacement(val !== '' ? Number(val) : val)}
225+
onChange={(val) =>
226+
setPlacement(() => ({
227+
values: [...val.map((x) => Number(x))],
228+
operator: 'in'
229+
}))
230+
}
231+
clearable
216232
data={placementsData}
217233
size="sm"
218234
/>
@@ -221,7 +237,20 @@ const SSPsAnalytics = ({
221237

222238
<Group grow gap="xl" align="baseline">
223239
<Stack>
224-
<Divider size="md" label="Categories" color="mainText" />
240+
<Divider
241+
mt="xl"
242+
labelPosition="left"
243+
label={
244+
<Center style={{ gap: 10 }}>
245+
<Text c="mainText" size="sm">
246+
Categories
247+
</Text>
248+
<ThemeIcon size="sm" variant="transparent" color="mainText">
249+
<InvoiceIcon />
250+
</ThemeIcon>
251+
</Center>
252+
}
253+
/>
225254
<MultiSelectAndRadioButtons
226255
onCategoriesChange={(selectedRadio, values) =>
227256
setCategories({
@@ -234,10 +263,24 @@ const SSPsAnalytics = ({
234263
defaultSelectValue={selectedCategories?.values}
235264
groups={CAT_GROUPS}
236265
label="Categories"
266+
size="sm"
237267
/>
238268
</Stack>
239269
<Stack>
240-
<Divider size="md" label="Countries" color="mainText" />
270+
<Divider
271+
mt="xl"
272+
labelPosition="left"
273+
label={
274+
<Center style={{ gap: 10 }}>
275+
<Text c="mainText" size="sm">
276+
Countries
277+
</Text>
278+
<ThemeIcon size="sm" variant="transparent" color="mainText">
279+
<GlobeIcon />
280+
</ThemeIcon>
281+
</Center>
282+
}
283+
/>
241284

242285
<MultiSelectAndRadioButtons
243286
onCategoriesChange={(selectedRadio, values) =>
@@ -251,6 +294,7 @@ const SSPsAnalytics = ({
251294
multiSelectData={COUNTRIES}
252295
groups={REGION_GROUPS}
253296
label="Countries"
297+
size="sm"
254298
/>
255299
</Stack>
256300
</Group>
@@ -285,10 +329,10 @@ const SSPsAnalytics = ({
285329
{JSON.stringify(
286330
{
287331
ssp,
288-
placement,
332+
placement: selectedPlacement,
289333
category: selectedCategories,
290334
country: selectedCountries,
291-
format,
335+
format: selectedFormats,
292336
groupBy
293337
},
294338
null,

src/components/CampaignAnalytics/CampaignAnalytics.tsx

+2-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { StickyPanel } from 'components/TopBar/TopBarStickyPanel'
88
import { AdminBadge } from 'components/common/AdminBadge'
99
import { useCampaignsData } from 'hooks/useCampaignsData'
1010
import SSPsAnalytics from 'components/AdminPanel/SSPsAnalytics'
11-
import { IabTaxonomyV3 } from 'adex-common'
11+
import { campaignDataToSSPAnalyticsQuery } from 'helpers'
1212
import Placements from './Placements'
1313
import Creatives from './Creatives'
1414
import SSPs from './SSPs'
@@ -91,33 +91,7 @@ const CampaignAnalytics = ({ isAdminPanel = false }: { isAdminPanel?: boolean })
9191
<SSPs campaignId={id} forAdmin={isAdminPanel} />
9292
</Tabs.Panel>
9393
<Tabs.Panel value="sspAnalytics">
94-
<SSPsAnalytics
95-
category={{
96-
values:
97-
campaign?.targetingInput.inputs.categories.apply === 'all'
98-
? []
99-
: (campaign?.targetingInput.inputs.categories[
100-
campaign?.targetingInput.inputs.categories.apply
101-
] as IabTaxonomyV3[]),
102-
operator:
103-
campaign?.targetingInput.inputs.categories.apply === 'all'
104-
? undefined
105-
: campaign?.targetingInput.inputs.categories.apply
106-
}}
107-
country={{
108-
values:
109-
campaign?.targetingInput.inputs.location.apply === 'all'
110-
? []
111-
: campaign?.targetingInput.inputs.location[
112-
campaign?.targetingInput.inputs.location.apply
113-
],
114-
operator:
115-
campaign?.targetingInput.inputs.location.apply === 'all'
116-
? undefined
117-
: campaign?.targetingInput.inputs.location.apply
118-
}}
119-
format={campaign?.adUnits.map((x) => `${x.banner?.format.h}x${x.banner?.format.w}`)}
120-
/>
94+
<SSPsAnalytics {...(campaign ? campaignDataToSSPAnalyticsQuery(campaign) : {})} />
12195
</Tabs.Panel>
12296
</Tabs>
12397
</Container>

src/components/CreateCampaign/StepThree/StepThree.tsx

+34-3
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,53 @@ import {
1010
TextInput,
1111
Tooltip
1212
} from '@mantine/core'
13-
import { useMemo } from 'react'
13+
import { useMemo, useState, useEffect } from 'react'
1414
import InfoFilledIcon from 'resources/icons/InfoFilled'
1515
import useCreateCampaignContext from 'hooks/useCreateCampaignContext'
1616
import InfoAlertMessage from 'components/common/InfoAlertMessage'
1717
import { parseRange } from 'helpers/createCampaignHelpers'
1818
import InfoIcon from 'resources/icons/Info'
1919
import DefaultCustomAnchor from 'components/common/customAnchor'
20+
import { campaignDataToSSPAnalyticsQuery } from 'helpers'
21+
import useSSPsAnalytics from 'hooks/useCampaignAnalytics/useSSPsAnalytics'
2022
import CampaignPeriod from './CampaignPeriod'
2123
import SelectCurrency from './SelectCurrency'
2224

2325
const StepThree = () => {
2426
const {
25-
campaign: { currency },
27+
campaign,
2628
selectedBidFloors,
2729
form: { key, getInputProps, errors, setFieldValue }
2830
} = useCreateCampaignContext()
2931

32+
const { analyticsData, getAnalyticsKeyAndUpdate } = useSSPsAnalytics()
33+
34+
const [analyticsKey, setAnalyticsKey] = useState<
35+
| {
36+
key: string
37+
}
38+
| undefined
39+
>()
40+
41+
useEffect(() => {
42+
setAnalyticsKey(undefined)
43+
44+
const checkAnalytics = async () => {
45+
const analKey = await getAnalyticsKeyAndUpdate({
46+
...campaignDataToSSPAnalyticsQuery(campaign),
47+
limit: 666
48+
})
49+
setAnalyticsKey(analKey)
50+
}
51+
52+
checkAnalytics()
53+
}, [campaign, getAnalyticsKeyAndUpdate])
54+
55+
const recommendedCPM = useMemo(
56+
() => analyticsData.get(analyticsKey?.key || '')?.data[0]?.value || 'N/A',
57+
[analyticsData, analyticsKey]
58+
)
59+
3060
const recommendedPaymentBounds = useMemo(() => {
3161
const rangeUnparsed = selectedBidFloors.flat().sort((a, b) => b.count - a.count)[0]?.value
3262

@@ -83,7 +113,7 @@ const StepThree = () => {
83113
3. Currency
84114
</Text>
85115
<SelectCurrency
86-
defaultValue={currency}
116+
defaultValue={campaign.currency}
87117
onChange={(value) => setFieldValue('currency', value)}
88118
error={(errors.currency && errors.currency) || ''}
89119
/>
@@ -144,6 +174,7 @@ const StepThree = () => {
144174
</ActionIcon>
145175
</Tooltip>
146176
</Group>
177+
{recommendedCPM}
147178

148179
<Group wrap="nowrap" justify="stretch" grow>
149180
<TextInput

0 commit comments

Comments
 (0)