Skip to content

Commit

Permalink
Mutualise et répare le fournisseur de tracking dans l'auth. et le panier
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-rutkowski committed Apr 25, 2024
1 parent fba7515 commit 8d7ca4c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 60 deletions.
37 changes: 7 additions & 30 deletions packages/auth/providers/posthog.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
'use client';

import posthog from 'posthog-js';
import {PostHogProvider} from 'posthog-js/react';
import {
TrackingProvider,
createTrackingClient,
getNextTrackingEnv,
} from '@tet/ui';

if (typeof window !== 'undefined') {
const is_dev = process.env.NODE_ENV === 'development';
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
const host = process.env.NEXT_PUBLIC_POSTHOG_HOST;

if (/*is_dev &&*/ !key || !host) {
console.warn(
`Le tracking PostHog n'est pas configuré, les variables d'env sont absentes.`,
);
} else {
if (!key) throw `La variable NEXT_PUBLIC_POSTHOG_KEY n'est pas définie`;
if (!host) throw `La variable NEXT_PUBLIC_POSTHOG_HOST n'est pas définie`;

// en mode dev, on envoie les données à PostHog, sinon on passe par le `rewrites` de `next.config.mjs`
const apiHost = is_dev ? host : `${window.location.origin}/ingest`;

posthog.init(key, {
api_host: apiHost,
ui_host: host,
capture_pageview: false, // on utilise PostHogPageView pour capturer les `page views`

loaded: posthog => {
if (process.env.NODE_ENV === 'development') posthog.debug();
},
});
}
}
const client = createTrackingClient(getNextTrackingEnv());

const PHProvider = ({children}: {children: React.ReactNode}) => {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
return <TrackingProvider client={client}>{children}</TrackingProvider>;
};

export default PHProvider;
37 changes: 7 additions & 30 deletions packages/panier/providers/posthog.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
'use client';

import posthog from 'posthog-js';
import {PostHogProvider} from 'posthog-js/react';
import {
TrackingProvider,
createTrackingClient,
getNextTrackingEnv,
} from '@tet/ui';

if (typeof window !== 'undefined') {
const is_dev = process.env.NODE_ENV === 'development';
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
const host = process.env.NEXT_PUBLIC_POSTHOG_HOST;

if (is_dev && (!key || !host)) {
console.warn(
`Le tracking PostHog n'est pas configuré, les variables d'env sont absentes.`,
);
} else {
if (!key) throw `La variable NEXT_PUBLIC_POSTHOG_KEY n'est pas définie`;
if (!host) throw `La variable NEXT_PUBLIC_POSTHOG_HOST n'est pas définie`;

// en mode dev, on envoie les données à PostHog, sinon on passe par le `rewrites` de `next.config.mjs`
const apiHost = is_dev ? host : `${window.location.origin}/ingest`;

posthog.init(key, {
api_host: apiHost,
ui_host: host,
capture_pageview: false, // on utilise PostHogPageView pour capturer les `page views`

loaded: posthog => {
if (process.env.NODE_ENV === 'development') posthog.debug();
},
});
}
}
const client = createTrackingClient(getNextTrackingEnv());

export const PHProvider = ({children}: {children: React.ReactNode}) => {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
return <TrackingProvider client={client}>{children}</TrackingProvider>;
};
67 changes: 67 additions & 0 deletions packages/ui/src/components/tracking/TrackingProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import posthog, {PostHog} from 'posthog-js';
import {PostHogProvider} from 'posthog-js/react';

/**
* Renvoi les vars d'env. pour le tracking depuis un module next js
*/
export const getNextTrackingEnv = () => {
const host = process.env.NEXT_PUBLIC_POSTHOG_HOST;
const key = process.env.NEXT_PUBLIC_POSTHOG_KEY;
const env = process.env.NODE_ENV;

return {host, key, env};
};

/**
* Crée le client de tracking
*/
export const createTrackingClient = ({
host,
key,
env,
}: {
host?: string;
key?: string;
env?: string;
}) => {
const is_dev = env === 'development';

if (!key || !host) {
console.warn(
`Le tracking PostHog n'est pas configuré, les variables d'env sont absentes.`
);
return;
}

// en mode dev, on envoie les données à PostHog, sinon on passe par le `rewrites` de `next.config.mjs`
const apiHost = is_dev ? host : `${window.location.origin}/ingest`;

posthog.init(key, {
api_host: apiHost,
ui_host: host,
capture_pageview: false, // on utilise PostHogPageView pour capturer les `page views`

loaded: posthog => {
if (process.env.NODE_ENV === 'development') posthog.debug();
},
});

return posthog;
};

/**
* Encapsule le client de tracking dans un provider react
*/
export const TrackingProvider = ({
children,
client,
}: {
children: React.ReactNode;
client: PostHog;
}) => {
return typeof window !== 'undefined' ? (
<PostHogProvider client={client}>{children}</PostHogProvider>
) : (
children
);
};
1 change: 1 addition & 0 deletions packages/ui/src/components/tracking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './trackingPlan';
export * from './useEventTracker';
export * from './useOngletTracker';
export * from './TrackPageView';
export * from './TrackingProvider';

0 comments on commit 8d7ca4c

Please sign in to comment.