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 daily visits stats (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
cberthou authored Mar 9, 2021
1 parent 5a8b74f commit 4605236
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ArchifiltreCountStatistic = {
label: string;
value: number;
value: Record<string, number> | number;
};
1 change: 1 addition & 0 deletions src/matomo/matomo-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const matomoConfig: MatomoSiteConfig[] = [
],
events: ["download", "appDownload"],
idSite: 20,
last30visits: true,
monthlyEvents: ["download", "appDownload"],
visits: true,
},
Expand Down
1 change: 1 addition & 0 deletions src/matomo/matomo-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export type MatomoSiteConfig = {
events?: MatomoEventConfig[];
actions?: MatomoActionConfigObject[];
monthlyEvents?: MatomoEventConfig[];
last30visits?: boolean;
visits?: boolean;
};
13 changes: 13 additions & 0 deletions src/matomo/matomo-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { MatomoEventCategory, MatomoSiteConfig } from "./matomo-types";
import {
createMatomoDataSanitizer,
getBulkRequestParamsFromConfig,
normalizeRequestDate,
} from "./matomo-utils";

type TestData =
Expand Down Expand Up @@ -108,4 +109,16 @@ describe("matomoUtils", () => {
]);
});
});

describe("normalizeRequestDate", () => {
it("should return a valid date string with an array of dates", () => {
expect(normalizeRequestDate(["yesterday", "today"])).toEqual(
"yesterday,today"
);
});

it("should return a valid date string with a single date", () => {
expect(normalizeRequestDate("yesterday")).toEqual("yesterday");
});
});
});
23 changes: 19 additions & 4 deletions src/matomo/matomo-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const sanitizeMatomoEventConfig = (
}
: config;

export const normalizeRequestDate = (date: string | [string, string]) =>
isString(date) ? date : date.join(",");

const createMatomoRequestBaseParams = (
idSite: number,
date: [string, string] = ["2020-01-01", "today"]
date: string | [string, string] = ["2020-01-01", "today"]
): Record<string, number | string> => ({
date: date.join(","),
date: normalizeRequestDate(date),
idSite,
period: "range",
});
Expand Down Expand Up @@ -72,10 +75,11 @@ const createMatomoEventActionMethod = ({
method: "Events.getActionFromCategoryId",
});

const createMatomoVisitMethod = (idSite: number): string =>
const createMatomoVisitMethod = (idSite: number, date?: string): string =>
querystring.stringify({
...createMatomoRequestBaseParams(idSite),
...createMatomoRequestBaseParams(idSite, date),
method: "VisitsSummary.getVisits",
period: date ? "day" : "range",
});

type RequestParams = Record<string, string>;
Expand All @@ -99,6 +103,7 @@ export const getBulkRequestParamsFromConfig = ({
events = [],
actions = [],
monthlyEvents = [],
last30visits = false,
visits = false,
idSite,
}: MatomoSiteConfig): RequestParams =>
Expand All @@ -113,6 +118,7 @@ export const getBulkRequestParamsFromConfig = ({
createMonthlyEventMethod({ config, idSite })
),
...(visits ? [createMatomoVisitMethod(idSite)] : []),
...(last30visits ? [createMatomoVisitMethod(idSite, "last30")] : []),
].reduce(
(urlParams, urlParam, index) => ({
...urlParams,
Expand Down Expand Up @@ -157,11 +163,19 @@ const formatVisitsResponse = () => (
value: number
): ArchifiltreCountStatistic => ({ label: "visitsCount", value });

const formatLastVisitsResponse = () => (
visitsMap: Record<string, number>
): ArchifiltreCountStatistic => ({
label: "last30DaysVisits",
value: visitsMap,
});

export const createMatomoDataSanitizer = ({
events = [],
actions = [],
monthlyEvents = [],
visits = false,
last30visits = false,
}: MatomoSiteConfig) => (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
matomoApiResponse: any[]
Expand All @@ -171,4 +185,5 @@ export const createMatomoDataSanitizer = ({
...actions.map(formatEventsOrActionsResponse),
...monthlyEvents.flatMap(formatMonthlyEvents),
...(visits ? [formatVisitsResponse()] : []),
...(last30visits ? [formatLastVisitsResponse()] : []),
].flatMap((formatter, index) => formatter(matomoApiResponse[index]));

0 comments on commit 4605236

Please sign in to comment.