Skip to content

Commit

Permalink
Set up Matomo for analytics/tracking (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Herrmann authored Aug 23, 2023
1 parent 0cb17e4 commit 90cb697
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
NEXT_PUBLIC_API_BASE_URL=http://localhost:3000/api

NEXT_PUBLIC_MATOMO_URL=https://piwik.technologiestiftung-berlin.de
NEXT_PUBLIC_MATOMO_SITE_ID=1234
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"@emotion/react": "11.11.1",
"@emotion/styled": "11.11.0",
"@socialgouv/matomo-next": "1.6.1",
"lucide-react": "^0.263.1",
"next": "13.4.7",
"next-intl": "^2.17.5",
Expand Down
12 changes: 10 additions & 2 deletions src/components/HomePage/NewsletterSection/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from "@emotion/styled";
import { useTranslations } from "next-intl";
import { borderWidths, colors, spacings } from "../../../common/styleVariables";
import { trackEvent } from "../../../services/analytics";
import ButtonWithIcon from "../../ButtonWithIcon";
import Spacer from "../../Spacer";
import Text from "../../Text";
Expand All @@ -21,8 +22,15 @@ const TextContainer = styled.div({
flex: "1 1 auto",
});

export default function NewsletterSection() {
type Props = {
trackingPosition: string;
};

export default function NewsletterSection({ trackingPosition }: Props) {
const t = useTranslations("Home.newsletter-section");
const trackButtonClick = () => {
trackEvent("Homepage", "Click 'Contact us' button", trackingPosition);
};
return (
<Container>
<ImageContainer>
Expand All @@ -34,7 +42,7 @@ export default function NewsletterSection() {
{t("title")}
</Text>
<Spacer size={16} />
<ButtonWithIcon as="a" icon="mail" href="mailto:[email protected]">
<ButtonWithIcon as="a" icon="mail" href="mailto:[email protected]" onClick={trackButtonClick}>
{t("button")}
</ButtonWithIcon>
</TextContainer>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useTranslations } from "next-intl";
import { useCallback, useState } from "react";
import { trackEvent } from "../../../../services/analytics";
import { getRandomIndexWithout } from "../../../../services/arrays";
import ButtonWithIcon from "../../../ButtonWithIcon";
import Spacer from "../../../Spacer";
Expand Down Expand Up @@ -30,6 +31,7 @@ export default function RequestCreator({ onStartRequestCreation, onRequestCreate
const handleCreateRequest = useCallback(() => {
onStartRequestCreation();
randomizeRequest();
trackEvent("Homepage", "Create random request");
}, [onStartRequestCreation, randomizeRequest]);
const handleAnimationFinished = useCallback(() => {
if (request) {
Expand Down
8 changes: 4 additions & 4 deletions src/components/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ export default function HomePage() {
<IntroSection />
<RequestCreatorAndList />
</Section>
<NewsletterSection />
<NewsletterSection trackingPosition="first" />
<Section ref={artistSectionRef} id={AnchorLinks.ARTISTSECTION}>
<ArtistSection />
</Section>
<NewsletterSection />
<NewsletterSection trackingPosition="second" />
<Section ref={interestedSectionRef} id={AnchorLinks.INTERESTEDSECTION}>
<CultureInterestedSection />
</Section>
<NewsletterSection />
<NewsletterSection trackingPosition="third" />
<Section ref={dataSectionRef} id={AnchorLinks.DATASECTION}>
<DataUsersSection />
</Section>
<NewsletterSection />
<NewsletterSection trackingPosition="fourth" />
</Page>
);
}
17 changes: 17 additions & 0 deletions src/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { init } from "@socialgouv/matomo-next";
import { useEffect, useRef } from "react";

const MATOMO_URL = process.env.NEXT_PUBLIC_MATOMO_URL as string;
const MATOMO_SITE_ID = process.env.NEXT_PUBLIC_MATOMO_SITE_ID as string;

export function useAnalytics() {
const matomoInitialized = useRef(false);
useEffect(() => {
if (MATOMO_URL && MATOMO_SITE_ID && matomoInitialized.current === false) {
init({ url: MATOMO_URL, siteId: MATOMO_SITE_ID });
}
return () => {
matomoInitialized.current = true;
};
}, []);
}
2 changes: 2 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { NextIntlClientProvider } from "next-intl";
import type { AppProps } from "next/app";
import "react-tooltip/dist/react-tooltip.css";
import GlobalStyles from "../components/GlobalStyles";
import { useAnalytics } from "../hooks/useAnalytics";

type CustomPageProps = {
messages: IntlMessages;
};

export default function App({ Component, pageProps }: AppProps<CustomPageProps>) {
useAnalytics();
return (
<NextIntlClientProvider messages={pageProps.messages}>
<GlobalStyles />
Expand Down
14 changes: 14 additions & 0 deletions src/services/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { push } from "@socialgouv/matomo-next";

type EventCategory = "Homepage";

/**
* Track a custom analytics event via Matomo.
* Docs: https://matomo.org/faq/reports/implement-event-tracking-with-matomo/
*/
export function trackEvent(eventCategory: EventCategory, eventAction: string, eventName?: string, eventValue?: number) {
const filledParameters = ["trackEvent", eventCategory, eventAction, eventName, eventValue].filter(
(parameter) => parameter !== undefined
);
push(filledParameters);
}

0 comments on commit 90cb697

Please sign in to comment.