Installation
This guide covers the complete installation process for Recapt using the script tag method.
Overview
Installing Recapt is a simple three-step process:
- Generate your API key
- Configure allowed domains
- Add the script tag to your website
Step 1: Generate Your API Key
- Log in to your Recapt dashboard
- Go to Installation in the sidebar
- Click Generate to create a new API key
- Your API key will be displayed - keep this safe
If you regenerate your API key, your existing installation will stop working until you update the key in your website's code.
Step 2: Configure Allowed Domains
For security, Recapt only accepts data from whitelisted domains.
- In the Installation wizard, go to Domain Settings
- Enter your domain (e.g.,
example.com) - Click Add Domain
- Repeat for all domains and subdomains
Examples of domains to add:
example.com- Your main domainwww.example.com- WWW subdomainapp.example.com- Application subdomainstaging.example.com- Staging environment
Step 3: Add the Script
Add this script tag to your website's <head> section:
<script
src="https://cdn.recapt.app/browser/glimt.js"
async
data-public-key="YOUR_API_KEY"
data-persist
></script>
Replace YOUR_API_KEY with the API key you generated in Step 1.
Script Attributes
| Attribute | Required | Description |
|---|---|---|
src | Yes | The Recapt CDN URL |
data-public-key | Yes | Your API key |
data-persist | No | Keeps sessions alive across page reloads |
async | No | Loads the script asynchronously (recommended) |
Complete HTML Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>My Website</title>
<!-- Recapt Installation -->
<script
src="https://cdn.recapt.app/browser/glimt.js"
async
data-public-key="pk_live_abc123..."
data-persist
></script>
</head>
<body>
<!-- Your content -->
</body>
</html>
Framework-Specific Examples
React / Next.js (Recommended Approach)
For React and Next.js applications, we recommend dynamically loading the Recapt script using a setup component. This gives you full control over initialization and user identification.
Create a RecaptSetup.tsx component:
"use client";
import { useEffect, useRef, useState } from "react";
// Extend Window interface for TypeScript
declare global {
interface Window {
Recapt: any;
recapt: (action: string, data: any) => void;
}
}
export default function RecaptSetup() {
const [isScriptLoaded, setIsScriptLoaded] = useState(false);
const retryCount = useRef(0);
const maxRetries = 50; // 5 seconds total (50 * 100ms)
// Load the Recapt script
useEffect(() => {
if (typeof window === "undefined") return;
const script = document.createElement("script");
script.src = "https://cdn.recapt.app/browser/glimt.js";
script.async = true;
// Configure via data attributes
script.setAttribute("data-public-key", "YOUR_API_KEY");
script.setAttribute("data-persist", "");
script.onload = () => {
setIsScriptLoaded(true);
};
script.onerror = () => {
console.error("Failed to load Recapt script");
};
document.body.appendChild(script);
return () => {
try {
document.body.removeChild(script);
} catch (error) {
// Script might already be removed
}
};
}, []);
// Wait for Recapt to be available, then identify user
useEffect(() => {
if (!isScriptLoaded) return;
const waitForRecapt = () => {
if (!window.Recapt) {
if (retryCount.current < maxRetries) {
retryCount.current++;
setTimeout(waitForRecapt, 100);
} else {
console.warn("Recapt not available after 5 seconds");
}
return;
}
// Recapt is ready - identify your user here
// Replace with your actual user data
const user = getCurrentUser(); // Your auth logic
if (user) {
window.recapt("identify", {
uid: user.id,
email: user.email,
nickname: user.nickname,
});
}
};
waitForRecapt();
}, [isScriptLoaded]);
return null;
}
Then include it in your app's root layout:
// app/layout.tsx (Next.js App Router)
import RecaptSetup from "@/components/RecaptSetup";
export default function RootLayout({ children }) {
return (
<html>
<body>
<RecaptSetup />
{children}
</body>
</html>
);
}
Identifying Users on Auth Changes
To identify users when they log in or out, integrate with your auth provider:
"use client";
import { useEffect, useRef, useState } from "react";
import { useAuth } from "./your-auth-provider"; // Your auth hook
export default function RecaptSetup() {
const { user, isLoading } = useAuth();
const [isScriptLoaded, setIsScriptLoaded] = useState(false);
const retryCount = useRef(0);
const maxRetries = 50;
// Load script effect (same as above)
useEffect(() => {
if (typeof window === "undefined") return;
const script = document.createElement("script");
script.src = "https://cdn.recapt.app/browser/glimt.js";
script.async = true;
script.setAttribute("data-public-key", "YOUR_API_KEY");
script.setAttribute("data-persist", "");
script.onload = () => setIsScriptLoaded(true);
script.onerror = () => console.error("Failed to load Recapt script");
document.body.appendChild(script);
return () => {
try {
document.body.removeChild(script);
} catch {}
};
}, []);
// Identify user when auth state changes
useEffect(() => {
if (!isScriptLoaded || isLoading) return;
const waitForRecapt = () => {
if (!window.Recapt) {
if (retryCount.current < maxRetries) {
retryCount.current++;
setTimeout(waitForRecapt, 100);
}
return;
}
retryCount.current = 0;
if (user) {
// User is logged in
window.recapt("identify", {
uid: user.id,
email: user.email,
nickname: user.nickname,
});
} else {
// User logged out - clear identification
window.recapt("identify", {
uid: undefined,
email: undefined,
nickname: undefined,
});
}
};
waitForRecapt();
}, [user, isLoading, isScriptLoaded]);
return null;
}
Vue.js
Add the script tag to your index.html:
<!-- public/index.html -->
<!DOCTYPE html>
<html>
<head>
<script
src="https://cdn.recapt.app/browser/glimt.js"
async
data-public-key="YOUR_API_KEY"
data-persist
></script>
</head>
<body>
<div id="app"></div>
</body>
</html>
Single Page Applications (SPAs)
For SPAs, the data-persist attribute is important to maintain session continuity across route changes. The script automatically handles navigation events within your application.
The dynamic script loading approach shown in the React/Next.js example above works well for any SPA framework. It ensures the script loads properly and gives you control over when to identify users.
Development Environment
By default, Recapt won't record sessions from localhost. To enable recording during development:
- Go to Settings > Organization in your dashboard
- Toggle Localhost to enable
- Sessions from
localhostwill now be recorded
We recommend keeping localhost disabled in production environments and only enabling it during active development.
Verifying Your Installation
After installation, verify that Recapt is working:
- Check the browser console: You should not see any Recapt-related errors
- Visit your Recapt dashboard: Navigate to Sessions
- Look for your session: New sessions typically appear within 1-2 minutes
If you don't see sessions appearing, check:
- Your API key is correct
- Your domain is whitelisted
- There are no JavaScript errors in the console
Troubleshooting
Script Not Loading
- Check that the script URL is correct:
https://cdn.recapt.app/browser/glimt.js - Verify there are no Content Security Policy (CSP) issues blocking the script
- Check your browser's Network tab for failed requests
Sessions Not Appearing
- Verify your domain is in the whitelist
- Check that the API key matches your organization
- Ensure you're not on localhost (unless localhost is enabled)
- Wait a few minutes - sessions may take 1-2 minutes to appear
Console Errors
If you see errors related to Recapt:
- Check that your API key is valid
- Verify the domain whitelist configuration
- Ensure the script is loaded before trying to use Recapt APIs
Next Steps
- User Identification - Associate sessions with your users
- Automatic Obfuscation - Protect sensitive data
- Painpoints - Control which sessions to keep