Skip to content
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

Closed
akshaysr-buku opened this issue Jan 4, 2022 · 4 comments

Comments

@akshaysr-buku
Copy link

akshaysr-buku commented Jan 4, 2022

Whenver i use Clevertap in NextJs _app.tsx
i am getting the below error
Screenshot 2022-01-04 at 7 02 21 PM

If the package isn't compatible with Next.js, is there any way I cal initialize clevertap in my App?

@akshaysr-buku akshaysr-buku changed the title ReferenceError: window is not defined Getting ReferenceError: window is not defined, when clevertap is used in _app.tsx file of NextJs Jan 4, 2022
@akshaysr-buku akshaysr-buku changed the title Getting ReferenceError: window is not defined, when clevertap is used in _app.tsx file of NextJs Getting ReferenceError: window is not defined, when clevertap is initialised in _app.tsx file of NextJs Jan 4, 2022
@akshaysr-buku
Copy link
Author

@akashvercetti

@akashvercetti
Copy link
Collaborator

Hi @akshaysr-buku , our SDK is a typically a client side only library and does not by its nature support SSR.
Having said that, there might be a workaround here: #39
or
you could try npm install --save-dev @types/clevertap-web-sdk since type-definitions have been created at: DefinitelyTyped/DefinitelyTyped#57546.

@abhushanaj
Copy link

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.

  1. Create an analytics component, which uses the Next.js Script component to inject the script
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});` }}
	/>
      )
 
}
  1. You can use this component using the dynamic import in _app.tsx
const Analytics = dynamic(() => import('@components/analytics'), { ssr: false });


function MyApp({ Component: AppComponent, pageProps }: AppProps) {
	return (
			<AppComponent {...pageProps} />
			<Analytics />		
	);
}
  1. Using clever tap to send events etc.

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!

@akshaysr-buku
Copy link
Author

Thanks @abhu-A-J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants