Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
feat(matomo): add matomo visitor countries (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alezco authored Mar 16, 2021
1 parent e2dd470 commit adc7827
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/matomo/matomo-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const matomoConfig: MatomoSiteConfig[] = [
"Audit report export",
],
idSite: 9,
visitorCountries: true,
},
{
actions: [
Expand Down
7 changes: 7 additions & 0 deletions src/matomo/matomo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ export type MatomoSiteConfig = {
monthlyEvents?: MatomoEventConfig[];
last30visits?: boolean;
visits?: boolean;
visitorCountries?: boolean;
};

export type MatomoUserCountry = {
code: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
nb_visits: number;
};
22 changes: 22 additions & 0 deletions src/matomo/matomo-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
MatomoEventConfig,
MatomoEventConfigObject,
MatomoSiteConfig,
MatomoUserCountry,
} from "./matomo-types";

const MONTHS_REQUESTED = 12;
Expand Down Expand Up @@ -82,6 +83,12 @@ const createMatomoVisitMethod = (idSite: number, date?: string): string =>
period: date ? "day" : "range",
});

const createMatomoVisitorCountriesMethod = (idSite: number): string =>
querystring.stringify({
...createMatomoRequestBaseParams(idSite),
method: "UserCountry.getCountry",
});

type RequestParams = Record<string, string>;

const getMatomoLastMonthsRange = getLastMonthsRanges(MONTHS_REQUESTED);
Expand All @@ -105,6 +112,7 @@ export const getBulkRequestParamsFromConfig = ({
monthlyEvents = [],
last30visits = false,
visits = false,
visitorCountries = false,
idSite,
}: MatomoSiteConfig): RequestParams =>
[
Expand All @@ -118,6 +126,7 @@ export const getBulkRequestParamsFromConfig = ({
createMonthlyEventMethod({ config, idSite })
),
...(visits ? [createMatomoVisitMethod(idSite)] : []),
...(visitorCountries ? [createMatomoVisitorCountriesMethod(idSite)] : []),
...(last30visits ? [createMatomoVisitMethod(idSite, "last30")] : []),
].reduce(
(urlParams, urlParam, index) => ({
Expand Down Expand Up @@ -163,6 +172,17 @@ const formatVisitsResponse = () => (
value: number
): ArchifiltreCountStatistic => ({ label: "visitsCount", value });

const formatVisitorCountriesResponse = () => (
countries: MatomoUserCountry[]
): ArchifiltreCountStatistic => ({
label: "visitorCountries",
value: countries.reduce(
// eslint-disable-next-line @typescript-eslint/naming-convention
(acc, { nb_visits, code }) => ({ ...acc, [code]: nb_visits }),
{}
),
});

const formatLastVisitsResponse = () => (
visitsMap: Record<string, number>
): ArchifiltreCountStatistic => ({
Expand All @@ -175,6 +195,7 @@ export const createMatomoDataSanitizer = ({
actions = [],
monthlyEvents = [],
visits = false,
visitorCountries = false,
last30visits = false,
}: MatomoSiteConfig) => (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -185,5 +206,6 @@ export const createMatomoDataSanitizer = ({
...actions.map(formatEventsOrActionsResponse),
...monthlyEvents.flatMap(formatMonthlyEvents),
...(visits ? [formatVisitsResponse()] : []),
...(visitorCountries ? [formatVisitorCountriesResponse()] : []),
...(last30visits ? [formatLastVisitsResponse()] : []),
].flatMap((formatter, index) => formatter(matomoApiResponse[index]));

0 comments on commit adc7827

Please sign in to comment.