Open EjDadivas opened 10 months ago
Also, if there's a way to initialize it on _app.js so I don't have to initialize it on every page
Hello @EjDadivas . Agreed that Next js doesn't work with jsx in the same way as it works with tsx. The index.jsx file will be updated as below
import { useState, useEffect } from 'react';
import dynamic from 'next/dynamic';
const CleverTapComponent = dynamic(() => import('clevertap-web-sdk/clevertap'), {
ssr: false, // This ensures CleverTap is not imported during server-side rendering
});
export default function Home() {
const [clevertapModule, setClevertapModule] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
// Fetch data only on the client side
const response = await fetch('/clevertap/upload', {
method: 'POST',
headers: {
'X-CleverTap-Account-Id': 'W9R-486-4W5Z',
'X-CleverTap-Passcode': '96921fadbf3445a7b416039d0e9d046d',
'Content-Type': 'application/json',
},
body: JSON.stringify(profileData)
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
};
// Check if the code is running on the client side
if (typeof window !== 'undefined') {
clevertapInit();
fetchData();
}
}, []);
const clevertapInit = async () => {
// Check if CleverTap module is already initialized
let clevertap = clevertapModule;
if (!clevertap) {
clevertap = await import('clevertap-web-sdk/clevertap');
clevertap.default.init("ZWW-WWW-WWRZ");
clevertap.default.privacy.push({ optOut: false });
clevertap.default.privacy.push({ useIP: false });
clevertap.default.setLogLevel(3);
setClevertapModule(clevertap.default);
}
if (clevertap) {
clevertap.event.push('CM 3333'); // Popup Campaign
clevertap.event.push('webpopup test'); // Banner Campaign
clevertap.event.push('InternalTest2'); // Inbox Campaign
}
};
const profileData = {
"d": [
{
"identity": "345543",
"type": "profile",
"profileData": {
"Name": "John Doe",
"Email": "john03aug@abc.com"
}
}
]
};
return (
<div>
{/* Navigation Bar */}
<nav style={{ backgroundColor: '#333', padding: '10px', color: '#fff' }}>
<ul style={{ listStyle: 'none', display: 'flex', justifyContent: 'space-between', maxWidth: '300px' }}>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li id='inbox'>Inbox</li>
</ul>
</nav>
{/* Hero Section */}
<section style={{ backgroundColor: '#f2f2f2', padding: '140px 0', textAlign: 'center' }}>
<div>
<h1 style={{ fontSize: '36px' }}>Welcome to Our Website</h1>
<p style={{ fontSize: '20px', color: '#666' }}>We provide amazing services for you.</p>
<button style={{ padding: '10px 20px', fontSize: '16px', background: '#007bff', color: '#fff', border: 'none', borderRadius: '5px', cursor: 'pointer' }}>
Get Started
</button>
</div>
</section>
{/* Clevertap Banner / carousel */}
<div id='heroDiv'></div>
{/* Call-to-action Section */}
<section style={{ backgroundColor: '#007bff', padding: '60px 0', textAlign: 'center', color: '#fff' }}>
<h2 style={{ fontSize: '30px' }}>Sign Up Now</h2>
<p style={{ fontSize: '18px' }}>Join us and experience the best services.</p>
<button style={{ padding: '10px 20px', fontSize: '16px', background: '#fff', color: '#007bff', border: 'none', borderRadius: '5px', cursor: 'pointer' }}>
Sign Up
</button>
</section>
</div>
);
}
For your query to initialize it on _app.js, give me some time I will work on it and revert. Thank You
@KambleSonam I am getting same issue. It would be great if you could find any solution for this.
Thanks in advance!
@Rohitdhende Kindly refer the repo https://github.com/PraveenCTzen/nextJs-demo/tree/main If this doesn't solve your issue, do let us know.
@KambleSonam thanks! It worked, but I believe this issue should be addressed within the package. Currently, we had to add extra lines of code for it.
@KambleSonam Hey, I am facing some issue. The event.push method isn't working properly, sometimes it works and sometimes it not. Don't know the exact reason, my observation is the scripts are not getting loaded after refresh.
And I have one more question that to use any event's method, we need to necessarily logon user?
1- Currently it working properly on our end, kindly raise a ticket to clevertap for debugging your issue.
2- No you don't need to do onuserlogin for events to work.
This happens when I don't use .tsx
in my package.json:
index.js