Skip to content

Commit

Permalink
fix: fix matomo implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
denisyilmaz committed Feb 28, 2024
1 parent e0a78ae commit eed2512
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-schools-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"yes-no-maybe-app": patch
---

fix: use working matomo trackPageView() implementation
6 changes: 3 additions & 3 deletions landingpage/src/providers/matomo/MatomoProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import MatomoTracker from "@jonkoops/matomo-tracker";

export const MatomoProvider = component$(() => {
const matomoState = useStore<MatomoInstanceState>({
instance: noSerialize(undefined),
matomo: noSerialize(undefined),
});
useContextProvider(MatomoContext, matomoState);

// eslint-disable-next-line qwik/no-use-visible-task
useVisibleTask$(() => {
Expand All @@ -39,9 +40,8 @@ export const MatomoProvider = component$(() => {
},
}),
);
matomoState.instance = _instance;
matomoState.matomo = noSerialize(_instance);
});
useContextProvider(MatomoContext, matomoState);
return <Slot />;
});

Expand Down
3 changes: 1 addition & 2 deletions landingpage/src/providers/matomo/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as MatomoContext } from './MatomoContext'
export { default as MatomoProvider } from './MatomoProvider'
export { default as createInstance } from './instance'
export { default as useMatomo } from './useMatomo'
export { default as createInstance } from './instance'
2 changes: 1 addition & 1 deletion landingpage/src/providers/matomo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ export type TrackSiteSearchParams = types.TrackSiteSearchParams
export type TrackLinkParams = types.TrackLinkParams

export interface MatomoInstanceState {
instance: NoSerialize<MatomoInstance>;
matomo: NoSerialize<MatomoInstance>;
}
30 changes: 23 additions & 7 deletions landingpage/src/routes/[...locale]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { $, component$, useSignal, useVisibleTask$ } from "@builder.io/qwik";
import {
$,
component$,
useContext,
useSignal,
useVisibleTask$,
} from "@builder.io/qwik";
import {
DocumentHead,
Link,
Expand All @@ -17,7 +23,7 @@ import {
useForm,
valiForm$,
} from "@modular-forms/qwik";
import { useMatomo } from "~/providers/matomo";
import { MatomoContext, useMatomo } from "~/providers/matomo";

const ContactSchema = object({
name: string([minLength(1, $localize`Please enter your name.`)]),
Expand All @@ -41,8 +47,8 @@ export const useFormLoader = routeLoader$<InitialValues<ContactForm>>(() => ({
// // Runs on server
// const { Client } = await import("node-mailjet");
// const mailjet = Client.apiConnect(
// process.env.MJ_APIKEY_PUBLIC ?? "xxx",
// process.env.MJ_APIKEY_PRIVATE ?? "xxx",
// process.env.MJ_APIKEY_PUBLIC ?? "76f4a9a03bb481c4f0ffbfab16b3c79b",
// process.env.MJ_APIKEY_PRIVATE ?? "afaa7a584eb54bf52c5452639d67524f",
// );
// const request = mailjet.post("send", { version: "v3.1" }).request({
// Messages: [
Expand Down Expand Up @@ -87,7 +93,14 @@ export const sendEmail = $((values: ContactForm) => {
});

export default component$(() => {
const { trackPageView, trackEvent } = useMatomo();
const { matomo } = useContext(MatomoContext);
// eslint-disable-next-line qwik/no-use-visible-task
useVisibleTask$(
() => {
matomo?.trackPageView();
},
{ strategy: "document-idle" },
);

const [, { Form, Field }] = useForm<ContactForm>({
loader: useFormLoader(),
Expand All @@ -97,15 +110,18 @@ export default component$(() => {

const handleSubmit = $<SubmitHandler<ContactForm>>((values) => {
// console.log(values);
trackEvent({ action: "submit", name: "contact-form", category: "form" });
matomo?.trackEvent({
action: "submit",
name: "contact-form",
category: "form",
});
sendEmail(values);
});

const showContent = useSignal<boolean>(false);
// eslint-disable-next-line qwik/no-use-visible-task
useVisibleTask$(() => {
showContent.value = true;
trackPageView();
});

return (
Expand Down

0 comments on commit eed2512

Please sign in to comment.