The Formo Web SDK is open source and implements the standard Events API.
Installation
IMPORTANT: To enable accurate attribution, install Formo on both your website (example.com) and your app (app.example.com) using the same project/SDK write key.
Websites
Install this snippet at the <head>
of your website:
<script
src="https://cdn.formo.so/analytics@latest"
defer onload="window.formofy('<YOUR_WRITE_KEY>');"
></script>
Enable Subresource Integrity (SRI) to improve site security.
React & Next.js
Install the JavaScript SDK via CDN or NPM.
npm install @formo/analytics --save
// App.tsx (or App.js)
import { FormoAnalyticsProvider } from '@formo/analytics';
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(
<React.StrictMode>
<FormoAnalyticsProvider writeKey="<YOUR_WRITE_KEY>">
<App />
</FormoAnalyticsProvider>
</React.StrictMode>
);
// Usage on a page component
import { useFormo } from '@formo/analytics';
const HomePage = () => {
const analytics = useFormo();
useEffect(() => {
// Track a custom event
analytics.track('Swap Completed', { points: 100 });
}, [analytics]);
return <div>Welcome to the Home Page!</div>;
};
export default HomePage;
Autocapture
The Web SDK automatically captures common events such as page views and wallet events (connect, disconnect, signature, transaction, etc) with full attribution (referrer, UTM, referrals.)
You do not need to configure anything to track these events.
Identify users
Call identify()
after a user connects their wallet or signs in on your website or app:
analytics.identify({ address });
OR
window.formo.identify(...);
If no parameters are specified, the Formo SDK will attempt to auto-identify the wallet address.
Track events
Track user actions by calling the track
function:
import { useFormo } from '@formo/analytics';
const analytics = useFormo();
analytics.track("Position Opened", // event name
{
pool_id: "LINK/ETH",
revenue: 99.99, // revenue
currency: "USD", // currency
points: 99.99 // points for XP or rewards
}
)
OR
window.formo.track(...)
Code examples
Configuration
Local testing
The SDK skips tracking in localhost by default.
To enable tracking locally during development, set tracking
to true
:
<AnalyticsProvider
writeKey={WRITE_KEY}
options={{
tracking: true, // enable tracking on localhost
}}
>
Logging
Control the level of logs the SDK prints to the console with the following logLevel settings:
<AnalyticsProvider
writeKey={WRITE_KEY}
options={{
logger: {
enabled: true,
levels: ["error", "warn", "info"],
},
}}
>
Log Level | Description |
---|
trace | Shows the most detailed diagnostic information, useful for tracing program execution flow. |
debug | Shows all messages, including function context information for each public method the SDK invokes. |
info | Shows informative messages about normal application operation. |
warn | Default. Shows error and warning messages. |
error | Shows error messages only. |
Batching
To support high-performance environments, the SDK sends events in batches.
<AnalyticsProvider
writeKey={WRITE_KEY}
options={{
flushAt: 20, // defaults to batches of 20 events
flushInterval: 1000 * 60, // defaults to 1 min
}}
>
Customize this behavior with the flushAt
and flushInterval
configuration parameters.
Ready callback
The ready
callback function executes once the Formo SDK is fully loaded and ready to use. This is useful for performing initialization tasks or calling SDK methods that require the SDK to be ready.
<AnalyticsProvider
writeKey={WRITE_KEY}
options={{
ready: function(formo) {
// SDK is ready
console.log('Formo SDK is ready!');
formo.identify(); // Auto-identify user
},
}}
>
For Browser installations, you can use the ready callback in the onload
attribute:
<script
src="https://cdn.formo.so/analytics@latest"
defer
onload="
window.formofy('<YOUR_WRITE_KEY>', {
ready: function(formo) {
formo.identify();
}
});
"
></script>
Environments
You can control tracking behavior in different environments (test, staging) with the tracking
option:
// Initialize with a boolean (simple on/off)
const analytics = await FormoAnalytics.init('your-write-key', {
tracking: true // Enable tracking everywhere, including localhost
});
// Initialize only on production environment
const analytics = await FormoAnalytics.init('your-write-key', {
tracking: ENV === 'production'
});
// Initialize with an object for exclusion rules
const analytics = await FormoAnalytics.init('your-write-key', {
tracking: {
excludeHosts: ['stage-v2.puri.fi'], // Exclude tracking based on window.location.hostname
excludePaths: ['/test', '/debug'], // Exclude tracking based on window.location.pathname
excludeChains: [5] // Exclude tracking on Goerli testnet (5)
}
});
Specify exact hostnames and exact paths in exclusion lists.
Consent management
The Formo Web SDK includes simplified consent management functionality to help you comply with privacy regulations like GDPR, CCPA, and ePrivacy Directive.
Control user tracking preferences with simple opt-out and opt-in methods:
import { useFormo } from '@formo/analytics';
const analytics = useFormo();
// Check if user has opted out of tracking
console.log(analytics.hasOptedOutTracking())
// If user has not opted out, tracking is enabled
analytics.track('page_view');
// Opt out of tracking (stops all tracking for the user)
analytics.optOutTracking();
// Opt back into tracking (re-enables tracking for the user)
analytics.optInTracking();
For browser installations:
// Manage consent
window.formo.optOutTracking();
window.formo.optInTracking();