-
Notifications
You must be signed in to change notification settings - Fork 18
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
Getting ReferenceError: window is not defined, when clevertap is initialised in _app.tsx file of NextJs #52
Comments
Hi @akshaysr-buku , our SDK is a typically a client side only library and does not by its nature support SSR. |
Hi @akashvercetti we also use the clever tap at our organization which runs a Next.js app. We use it using the following strategy, without relying on the SDK.
import Script from 'next/script'
function Analytics(){
return (
<Script strategy="afterInteractive" id="clevertap-script" src="https://d2r1yp2w7bby2u.cloudfront.net/js/a.js" />
<Script id="clevertap-analytics" strategy="afterInteractive"
dangerouslySetInnerHTML={{ __html:`
var clevertap = {event:[], profile:[], account:[], onUserLogin:[], notifications:[], privacy:[]};
clevertap.account.push({"id": "${clevertapAccountId}"});
clevertap.privacy.push({optOut: false});
clevertap.privacy.push({useIP: true});` }}
/>
)
}
const Analytics = dynamic(() => import('@components/analytics'), { ssr: false });
function MyApp({ Component: AppComponent, pageProps }: AppProps) {
return (
<AppComponent {...pageProps} />
<Analytics />
);
}
If you are using typescript you can write your own types for payload depending on your use case. export type WindowWithAnalytics = Window &
typeof globalThis & {
clevertap: any; // write more types here if you want
};
class Analytics{
isWindowRuntime= typeof window !==='undefined'
pushEvent(eventName,payload){
(window as WindowWithAnalytics).clevertap.event.push(eventName, payload);
}
}
// usage
Analytics.pushEvent('Login',{
//details
})
Hopefully, this helps! |
Thanks @abhu-A-J |
Whenver i use Clevertap in NextJs _app.tsx
![Screenshot 2022-01-04 at 7 02 21 PM](https://user-images.githubusercontent.com/95604746/148066698-f1fbb75c-7d23-4f5e-b22e-4674bb85f4b0.png)
i am getting the below error
If the package isn't compatible with Next.js, is there any way I cal initialize clevertap in my App?
The text was updated successfully, but these errors were encountered: