Skip to content

Env file missing error #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: npm install && npm run build
command: npm run watch


34 changes: 33 additions & 1 deletion src/components/GoogleAnalytics.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,39 @@ describe("GoogleAnalytics", () => {
});
expect(Router.events.on).toBeCalled();
});

it('should not throw an error when no process.env exists', () => {
// These tests run in node however this component will also be rendered on the browser.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
process.env = undefined
render(
<GoogleAnalytics />
);
expect(usePageViewsSpy).toBeCalledWith({
disabled: true,
gaMeasurementId: undefined,
ignoreHashChange: false,
});
expect(Router.events.on).not.toBeCalled();
})
it('should work with the prop when no process.env exists', () => {
// These tests run in node however this component will also be rendered on the browser.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
process.env = undefined
render(
<GoogleAnalytics
trackPageViews={{ ignoreHashChange: false }}
gaMeasurementId="5678"
/>
);
expect(usePageViewsSpy).toBeCalledWith({
disabled: false,
gaMeasurementId: "5678",
ignoreHashChange: false,
});
expect(Router.events.on).toBeCalled();
})
describe("debugMode", () => {
it("should not have debug_mode when the debugMode prop is not set", () => {
render(<GoogleAnalytics gaMeasurementId="1234" />);
Expand Down
25 changes: 19 additions & 6 deletions src/components/GoogleAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ type WithIgnoreHashChange = GoogleAnalyticsProps & {
};
};

const getGaMeasurementId = (gaMeasurementIdProp?: string) => {
let gaMeasurementId: string | undefined
try {
// process.env could be undefined if no env. file or next.config is not using env
gaMeasurementId = process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID
} catch (e) {
// nothing to do
}
// To keep feature parity, allow the env var to override the prop
if (!gaMeasurementId) {
gaMeasurementId = gaMeasurementIdProp
}
return gaMeasurementId
}

export function GoogleAnalytics({
debugMode = false,
gaMeasurementId,
Expand All @@ -30,8 +45,7 @@ export function GoogleAnalytics({
trackPageViews,
nonce,
}: WithPageView | WithIgnoreHashChange): JSX.Element | null {
const _gaMeasurementId =
process.env.NEXT_PUBLIC_GA_MEASUREMENT_ID ?? gaMeasurementId;
const _gaMeasurementId = getGaMeasurementId(gaMeasurementId)

usePageViews({
gaMeasurementId: _gaMeasurementId,
Expand All @@ -54,13 +68,12 @@ export function GoogleAnalytics({
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
${
defaultConsent === "denied" ?
`gtag('consent', 'default', {
${defaultConsent === "denied" ?
`gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied'
});` : ``
}
}
gtag('config', '${_gaMeasurementId}', {
page_path: window.location.pathname,
${debugMode ? `debug_mode: ${debugMode},` : ""}
Expand Down