Skip to content

Commit

Permalink
Add example on how to expose the instance globally (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Dec 18, 2023
1 parent 1638ffd commit fc79b80
Show file tree
Hide file tree
Showing 10 changed files with 862 additions and 843 deletions.
848 changes: 416 additions & 432 deletions examples/next-csr-app/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/next-csr-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@croct/plug-react": "latest",
"next": "^12.3.3",
"next": "^14.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
19 changes: 19 additions & 0 deletions examples/next-ssr-app/components/GlobalCroct.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {render} from '@testing-library/react';
import croct, {Plug} from '@croct/plug';
import {GlobalCroct} from '@/components/GlobalCroct';

declare global {
interface Window {
croct?: Plug;
}
}

describe('<Providers />', () => {
it('should expose the Croct instance on the global scope', () => {
expect(window.croct).toBeUndefined();

render(<GlobalCroct />);

expect(window.croct).toBe(croct);
});
});
31 changes: 31 additions & 0 deletions examples/next-ssr-app/components/GlobalCroct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use client';

import {FunctionComponent, useEffect} from 'react';
import croct from '@croct/plug';

/*
* NOTE
*
* This component makes the `croct` instance available on the `window`
* object so tools like Google Tag Manager can use it.
*
* Although functional, we do not recommend this approach as it can
* lead to problems like race conditions where Google Tag Manager may
* run before the `useEffect` hook.
*
* For more reliable and efficient event tracking, use the `useCroct`
* hook directly in your application.
*/

export const GlobalCroct: FunctionComponent = () => {
useEffect(
() => {
if (!('croct' in window)) {
Object.assign(window, {croct: croct});
}
},
[],
);

return null;
};
2 changes: 1 addition & 1 deletion examples/next-ssr-app/components/Providers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('<Providers />', () => {
{
appId: APP_ID,
clientId: clientId,
children: 'foo',
children: expect.arrayContaining(['foo']),
},
expect.anything(),
);
Expand Down
2 changes: 2 additions & 0 deletions examples/next-ssr-app/components/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import {PropsWithChildren, ReactElement} from 'react';
import {CroctProvider} from '@croct/plug-react/CroctProvider';
import {headers} from 'next/headers';
import {Header} from '@/lib/constants';
import {GlobalCroct} from '@/components/GlobalCroct';

export default function Providers({children}: PropsWithChildren): ReactElement {
return (
<CroctProvider
appId={process.env.NEXT_PUBLIC_CROCT_APP_ID!}
clientId={headers().get(Header.CLIENT_ID) ?? undefined}
>
<GlobalCroct />
{children}
</CroctProvider>
);
Expand Down

0 comments on commit fc79b80

Please sign in to comment.