New Analytics Provider #2419
Replies: 2 comments 2 replies
-
|
In my local 'use client';
import { PropsWithChildren } from 'react';
import { FragmentOf } from '~/client/graphql';
import { Analytics } from '~/lib/analytics';
import { GoogleAnalyticsProvider } from '~/lib/analytics/providers/google-analytics';
import { AnalyticsProvider as AnalyticsProviderLib } from '~/lib/analytics/react';
import { WebAnalyticsFragment } from './fragment';
interface Props {
channelId: number;
settings?: FragmentOf<typeof WebAnalyticsFragment> | null;
}
const getAnalytics = (
channelId: number,
settings?: FragmentOf<typeof WebAnalyticsFragment> | null,
) => {
const gaId = settings?.webAnalytics?.ga4?.tagId ?? process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID;
if (gaId && channelId) {
const googleAnalytics = new GoogleAnalyticsProvider({
gaId,
// TODO: Need to implement consent mode
// https://github.com/bigcommerce/catalyst/issues/2066
consentModeEnabled: false,
developerId: 'dMjk3Nj',
});
return new Analytics({
channelId,
providers: [googleAnalytics],
});
}
return null;
};
export function AnalyticsProvider({ children, settings, channelId }: PropsWithChildren<Props>) {
const analytics = getAnalytics(channelId, settings);
return <AnalyticsProviderLib analytics={analytics ?? null}>{children}</AnalyticsProviderLib>;
} |
Beta Was this translation helpful? Give feedback.
-
@lachlan-connell-aligent Can you explain a bit more on the reasoning for wanting to use an environment variable here? With the existing latest code, adding the ID in the control panel will allow GA to work not just within Catalyst but in Checkout as well. If it's only provided through the environment variable, you'll also need to add it to the CP for it to work within Checkout. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
as requested by @chanceaclark, setting up a new discussion here.
while this is a good improvement:
It doesn't appear the option to load in the analytics from an environment variable is still available?
I'm looking at
core/components/analytics/provider.tsxI think it would be good to have either option available, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions