Web SDK
The Web SDK (@tolinku/web-sdk) provides event tracking, smart banners, in-app messages, referral management, and deferred deep link claiming for browser and Node.js environments.
Installation
Section titled “Installation”npm install @tolinku/web-sdkyarn add @tolinku/web-sdkpnpm add @tolinku/web-sdkimport { Tolinku } from '@tolinku/web-sdk';
const tolinku = new Tolinku({ apiKey: 'tolk_pub_your_key', baseUrl: 'https://your-app.tolinku.com' // optional});The baseUrl defaults to https://api.tolinku.com. Set it to your Appspace’s domain if you use a custom domain.
User identification
Section titled “User identification”Set a user ID for analytics attribution and segment targeting:
tolinku.setUserId('user_123');
// Clear the user ID (e.g. on logout)tolinku.setUserId(null);Event tracking
Section titled “Event tracking”Track custom events:
// Simple eventtolinku.track('custom.page_view');
// Event with propertiestolinku.track('custom.purchase', { campaign: 'spring-sale', source: 'email', user_id: 'user_123'});Events are batched automatically (10 events or every 5 seconds) and sent via navigator.sendBeacon on page unload to avoid losing data.
You can force an immediate flush:
await tolinku.flush();Ecommerce tracking
Section titled “Ecommerce tracking”Track purchases, cart activity, and product events via tolinku.ecommerce:
// Set user ID for attributiontolinku.setUserId('user_123');
// Track a purchaseawait tolinku.ecommerce.purchase({ transaction_id: 'order_456', revenue: 49.99, currency: 'USD', items: [ { item_id: 'sku_1', item_name: 'T-Shirt', price: 24.99, quantity: 2 } ]});
// Track product viewsawait tolinku.ecommerce.viewItem({ items: [{ item_id: 'sku_1', item_name: 'T-Shirt', price: 24.99 }]});
// Track add to cartawait tolinku.ecommerce.addToCart({ items: [{ item_id: 'sku_1', quantity: 1 }]});
// Track checkout flowawait tolinku.ecommerce.beginCheckout({});await tolinku.ecommerce.addPaymentInfo();
// Search and product interactionsawait tolinku.ecommerce.search({ search_term: 'red shoes' });await tolinku.ecommerce.rate({ item_id: 'sku_1', rating: 4.5, max_rating: 5 });Ecommerce events are batched separately from custom events (10 events or 5-second timer) and sent to the ecommerce batch endpoint. The SDK automatically manages a cart ID across cart events and clears it after purchase.
Smart banners
Section titled “Smart banners”Display a smart app banner on your web page:
// Show the highest-priority active bannerawait tolinku.showBanner();
// Filter by labelawait tolinku.showBanner({ label: 'summer-promo' });
// Dismiss the current bannertolinku.dismissBanner();In-app messages
Section titled “In-app messages”Display in-app messages:
// Show the highest-priority undismissed messageawait tolinku.showMessage();
// Filter by triggerawait tolinku.showMessage({ trigger: 'on_open' });
// Dismiss the current messagetolinku.dismissMessage();Messages are rendered as a DOM modal overlay.
Referrals
Section titled “Referrals”Access referral methods via tolinku.referrals:
// Create a referral codeconst { referral_code, referral_url } = await tolinku.referrals.create({ userId: 'user_123', userName: 'Jane Doe'});
// Look up a referralconst info = await tolinku.referrals.get('ABC123');
// Link a referred user (status stays pending until reward milestone is reached)await tolinku.referrals.complete({ code: 'ABC123', referredUserId: 'user_456'});
// Update milestone (completes the referral if it matches the reward milestone)await tolinku.referrals.milestone({ code: 'ABC123', milestone: 'first_purchase'});
// Claim reward (after granting it in your system)await tolinku.referrals.claimReward('ABC123');
// Get leaderboardconst { leaderboard } = await tolinku.referrals.leaderboard(10);Deferred deep links
Section titled “Deferred deep links”Claim deferred deep links after a user arrives from a deep link and installs your app:
// Token-based claiming (if you have a tolk_token from a referrer or URL parameter)const link = await tolinku.deferred.claimByToken(token);if (link) { // Navigate to link.deep_link_path}
// Signal-based claiming (fingerprint matching)const link = await tolinku.deferred.claimBySignals({ appspaceId: 'your_appspace_id'});For token-based claiming, the token is the tolk_token value from the Play Store install referrer (Android) or a URL parameter. Signal-based claiming automatically collects timezone, language, and screen dimensions from the browser.
Cleanup
Section titled “Cleanup”For single-page applications, clean up when unmounting:
tolinku.destroy();This flushes any queued events, removes DOM elements (banners, messages), and cancels pending requests.