Framework Integrations
The SDK is framework-agnostic — it works anywhere a browser runs. The recipes below show the cleanest integration pattern for the most common stacks.
React
Create a single SDK instance and share it via context. Identify in an effect that depends on your auth state.
// treliq.ts
import { init, type TreliqSDK } from '@treliq/sdk';
export const treliq: TreliqSDK = init({
apiKey: import.meta.env.VITE_TRELIQ_API_KEY,
projectId: import.meta.env.VITE_TRELIQ_PROJECT_ID,
autoTrack: true,
onError: (err) => console.error('[treliq]', err),
});
// TreliqProvider.tsx
import { useEffect } from 'react';
import { useAuth } from './auth';
import { treliq } from './treliq';
export function TreliqProvider({ children }: { children: React.ReactNode }) {
const { user } = useAuth();
useEffect(() => {
if (user) {
treliq.identify(user.id, { email: user.email, plan: user.plan });
} else {
treliq.reset();
}
}, [user]);
return <>{children}</>;
}
// Tracking events from any component
import { treliq } from './treliq';
function CheckoutButton() {
return (
<button onClick={() => treliq.track('checkout_started', { value: 79.99 })}>
Check out
</button>
);
}
Vue 3 (Composition API)
// composables/useTreliq.ts
import { init, type TreliqSDK } from '@treliq/sdk';
let instance: TreliqSDK | null = null;
export function useTreliq(): TreliqSDK {
if (!instance) {
instance = init({
apiKey: import.meta.env.VITE_TRELIQ_API_KEY,
projectId: import.meta.env.VITE_TRELIQ_PROJECT_ID,
autoTrack: true,
});
}
return instance;
}
<!-- App.vue -->
<script setup lang="ts">
import { watch } from 'vue';
import { useAuth } from './stores/auth';
import { useTreliq } from './composables/useTreliq';
const auth = useAuth();
const treliq = useTreliq();
watch(
() => auth.user,
async (user) => {
if (user) {
await treliq.identify(user.id, { email: user.email });
} else {
treliq.reset();
}
},
{ immediate: true },
);
</script>
Next.js (App Router)
The SDK is browser-only — never call init() from a server component or middleware. Wrap the entry in a 'use client' boundary.
// app/treliq-client.ts
'use client';
import { init, type TreliqSDK } from '@treliq/sdk';
let instance: TreliqSDK | null = null;
export function getTreliq(): TreliqSDK {
if (typeof window === 'undefined') {
throw new Error('Treliq SDK is browser-only');
}
if (!instance) {
instance = init({
apiKey: process.env.NEXT_PUBLIC_TRELIQ_API_KEY!,
projectId: process.env.NEXT_PUBLIC_TRELIQ_PROJECT_ID!,
autoTrack: true,
});
}
return instance;
}
// app/TreliqBootstrap.tsx
'use client';
import { useEffect } from 'react';
import { useSession } from 'next-auth/react';
import { getTreliq } from './treliq-client';
export default function TreliqBootstrap() {
const { data: session } = useSession();
useEffect(() => {
const treliq = getTreliq();
if (session?.user) {
treliq.identify(session.user.id, { email: session.user.email });
} else {
treliq.reset();
}
}, [session]);
return null;
}
// app/layout.tsx
import TreliqBootstrap from './TreliqBootstrap';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<TreliqBootstrap />
{children}
</body>
</html>
);
}
Use
NEXT_PUBLIC_*env vars — the API key is publishable and meant to ship to the browser.
Vanilla JavaScript
<script type="module">
import { init } from 'https://cdn.treliq.com/treliq.esm.js';
const treliq = init({
apiKey: 'tk_live_xxx',
projectId: 'your-project-id',
});
// After login
await treliq.identify('user_123', { email: '[email protected]' });
// On user action
document.querySelector('#cta')?.addEventListener('click', () => {
treliq.track('cta_clicked', { location: 'hero' });
});
</script>
For non-module setups, see CDN / Script Tag.
Server-side rendering caveats
The SDK references window, document, localStorage, WebSocket, and navigator.onLine. Calling init() on the server will throw or behave incorrectly. Always guard with:
'use client'(Next.js, Remix)<ClientOnly>wrapper (Nuxt, SvelteKit)if (typeof window !== 'undefined')(vanilla SSR)
The SDK does not currently provide a Node-side companion for server-emitted events. Server-side event tracking goes through the HTTP API directly with a secret API key.