diff --git a/pages/domain/console/[slug]/index.tsx b/pages/domain/console/[slug]/index.tsx index a824122..1df5819 100644 --- a/pages/domain/console/[slug]/index.tsx +++ b/pages/domain/console/[slug]/index.tsx @@ -32,6 +32,40 @@ const DiscoverPage: NextPage = () => { const theDomains: DomainType[] = (domainsData && domainsData.domains) || []; const theKeywords: SearchAnalyticsItem[] = keywordsData?.data && keywordsData.data[scDateFilter] ? keywordsData.data[scDateFilter] : []; + const theKeywordsCount = theKeywords.reduce>((r, o) => { + const key = `${o.device}-${o.country}-${o.keyword}`; + const item = r.get(key) || 0; + return r.set(key, item + 1); + }, new Map()) || []; + + const theKeywordsGrouped : SearchAnalyticsItem[] = [...theKeywords.reduce>((r, o) => { + const key = `${o.device}-${o.country}-${o.keyword}`; + + const item = r.get(key) || { ...o, + ...{ + clicks: 0, + impressions: 0, + ctr: 0, + position: 0, + }, + }; + item.clicks += o.clicks; + item.impressions += o.impressions; + item.ctr = o.ctr + item.ctr; + item.position = o.position + item.position; + + return r.set(key, item); + }, new Map()).values()].map((o: SearchAnalyticsItem) => { + const key = `${o.device}-${o.country}-${o.keyword}`; + const count = theKeywordsCount?.get(key) || 0; + return { ...o, + ...{ + ctr: o.ctr / count, + position: o.position / count, + }, + }; + }); + const activDomain: DomainType|null = useMemo(() => { let active:DomainType|null = null; if (domainsData?.domains && router.query?.slug) { @@ -62,7 +96,7 @@ const DiscoverPage: NextPage = () => { domains={theDomains} showAddModal={() => console.log('XXXXX')} showSettingsModal={setShowDomainSettings} - exportCsv={() => exportCSV(theKeywords, activDomain.domain, scDateFilter)} + exportCsv={() => exportCSV(theKeywordsGrouped, activDomain.domain, scDateFilter)} scFilter={scDateFilter} setScFilter={(item:string) => setSCDateFilter(item)} /> @@ -71,7 +105,7 @@ const DiscoverPage: NextPage = () => {