Intégration React / Next.js
Guide d'intégration pour les applications React et Next.js.
Next.js (App Router - Recommandé)
Méthode 1 : Script dans layout.tsx
// app/layout.tsx
import Script from 'next/script'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="fr">
<body>
{children}
{/* AdSentinel SDK */}
<Script
src="https://cdn.web-kit.io/loader.js?site=VOTRE_SITE_ID"
strategy="afterInteractive"
/>
</body>
</html>
)
}
Méthode 2 : Composant dédié
Créez un composant réutilisable :
// components/AdSentinel.tsx
'use client'
import { useEffect } from 'react'
interface AdSentinelProps {
siteId: string
debug?: boolean
}
export function AdSentinel({ siteId, debug = false }: AdSentinelProps) {
useEffect(() => {
// Configuration optionnelle
if (debug) {
(window as any).ADWALL_CONFIG = { debug: true }
}
// Charger le script
const script = document.createElement('script')
script.src = `https://cdn.web-kit.io/loader.js?site=${siteId}`
script.async = true
document.body.appendChild(script)
// Cleanup
return () => {
script.remove()
}
}, [siteId, debug])
return null
}
Utilisation :
// app/layout.tsx
import { AdSentinel } from '@/components/AdSentinel'
export default function RootLayout({ children }) {
return (
<html lang="fr">
<body>
{children}
<AdSentinel siteId="VOTRE_SITE_ID" />
</body>
</html>
)
}
Next.js (Pages Router)
// pages/_app.tsx
import Script from 'next/script'
import type { AppProps } from 'next/app'
export default function App({ Component, pageProps }: AppProps) {
return (
<>
<Component {...pageProps} />
{/* AdSentinel SDK */}
<Script
src="https://cdn.web-kit.io/loader.js?site=VOTRE_SITE_ID"
strategy="afterInteractive"
/>
</>
)
}
React (Create React App / Vite)
Méthode 1 : Dans index.html
<!-- public/index.html -->
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<title>Mon App React</title>
</head>
<body>
<div id="root"></div>
<!-- AdSentinel SDK -->
<script src="https://cdn.web-kit.io/loader.js?site=VOTRE_SITE_ID" async></script>
</body>
</html>
Méthode 2 : Hook personnalisé
// hooks/useAdSentinel.ts
import { useEffect } from 'react'
interface UseAdSentinelOptions {
siteId: string
debug?: boolean
onConverted?: () => void
onDetected?: () => void
}
export function useAdSentinel({
siteId,
debug = false,
onConverted,
onDetected
}: UseAdSentinelOptions) {
useEffect(() => {
// Configuration
if (debug) {
(window as any).ADWALL_CONFIG = { debug: true }
}
// Event listeners
const handleConverted = () => onConverted?.()
const handleDetected = () => onDetected?.()
window.addEventListener('adwall:converted', handleConverted)
window.addEventListener('adwall:detected', handleDetected)
// Charger le script
const script = document.createElement('script')
script.src = `https://cdn.web-kit.io/loader.js?site=${siteId}`
script.async = true
document.body.appendChild(script)
// Cleanup
return () => {
script.remove()
window.removeEventListener('adwall:converted', handleConverted)
window.removeEventListener('adwall:detected', handleDetected)
}
}, [siteId, debug, onConverted, onDetected])
}
Utilisation :
// App.tsx
import { useAdSentinel } from './hooks/useAdSentinel'
function App() {
useAdSentinel({
siteId: 'VOTRE_SITE_ID',
debug: process.env.NODE_ENV === 'development',
onConverted: () => {
console.log('Utilisateur converti !')
// Analytics, etc.
},
onDetected: () => {
console.log('Adblock détecté')
}
})
return (
<div className="App">
{/* Votre application */}
</div>
)
}
Types TypeScript
Pour une meilleure intégration TypeScript, ajoutez ces types :
// types/adwall.d.ts
interface AdSentinelConfig {
debug?: boolean
delay?: number
cookieDomain?: string | null
}
interface AdSentinelEventDetail {
sessionId: string
variant: string
timestamp: number
}
declare global {
interface Window {
ADWALL_CONFIG?: AdSentinelConfig
AdSentinel?: {
check: () => void
getSessionId: () => string
}
}
interface WindowEventMap {
'adwall:init': CustomEvent<AdSentinelEventDetail>
'adwall:detected': CustomEvent<AdSentinelEventDetail>
'adwall:displayed': CustomEvent<AdSentinelEventDetail>
'adwall:converted': CustomEvent<AdSentinelEventDetail>
'adwall:closed': CustomEvent<AdSentinelEventDetail>
'adwall:error': CustomEvent<{ error: string }>
}
}
export {}
Variables d'environnement
Utilisez des variables d'environnement pour gérer le Site ID :
# .env.local
NEXT_PUBLIC_ADWALL_SITE_ID=677c2846-fed7-4461-b69c-7717bc3be9b4
// components/AdSentinel.tsx
const siteId = process.env.NEXT_PUBLIC_ADWALL_SITE_ID
<Script
src={`https://cdn.web-kit.io/loader.js?site=${siteId}`}
strategy="afterInteractive"
/>
Désactiver en développement
// app/layout.tsx
import Script from 'next/script'
export default function RootLayout({ children }) {
const isProduction = process.env.NODE_ENV === 'production'
return (
<html lang="fr">
<body>
{children}
{/* AdSentinel uniquement en production */}
{isProduction && (
<Script
src="https://cdn.web-kit.io/loader.js?site=VOTRE_SITE_ID"
strategy="afterInteractive"
/>
)}
</body>
</html>
)
}
Conseil
En développement, activez le mode debug: true pour voir les logs du SDK dans la console.