Banners
The Tappd Web SDK supports displaying banners created in your Tappd dashboard. Banners are placement-based content that can be attached to specific DOM elements using CSS selectors or data attributes, with support for targeting, scheduling, frequency capping, and A/B testing.
Overview
Banners are different from in-app messages:
- Placement-based: Banners are attached to specific DOM elements (CSS selectors or data attributes)
- Targeting: Support for customer segments, rules, and targeting conditions
- Scheduling: Start/end dates, active hours, and timezone support
- Frequency Capping: Control how often banners are shown to users
- A/B Testing: Multiple variants with traffic allocation
- Analytics: Track displays, clicks, dismissals, and conversions
Quick Start
The SDK handles all API calls, rendering, and event tracking automatically:
import { TappdSDK } from '@tappd/web-sdk';
const tappd = new TappdSDK({
appId: 'YOUR_APP_ID',
apiUrl: 'https://sdk.gotappd.com/api/v1/sdk'
});
// Identify user first (required for targeting)
await tappd.identify({
external_id: 'user_123',
email: 'john@example.com',
name: 'John Doe'
});
// SDK handles everything - fetch, render, and track
await tappd.getBanners('#header-banner');That's it! The SDK will:
- Fetch eligible banners from the API
- Render them in the specified DOM element
- Automatically track display, click, and dismiss events
Banner Placement System
Banners use a placement system to attach to specific DOM elements. There are three placement types:
1. CSS Selector Placement
Banners can be placed using CSS selectors:
<!-- Your HTML -->
<header id="header-banner">
<!-- Banner will be injected here automatically -->
</header>// SDK automatically finds and renders in #header-banner
await tappd.getBanners('#header-banner');2. Data Attribute Placement
Banners can be placed using data attributes:
<!-- Your HTML -->
<div data-tappd-banner="promo">
<!-- Banner will be injected here automatically -->
</div>// SDK automatically finds and renders in data-tappd-banner="promo"
await tappd.getBanners('data-tappd-banner="promo"');3. Both Placement Types
Banners can match both CSS selector and data attribute:
// Banner configured with placement.type: 'both'
// Matches both CSS selector and data attributeAuto-Rendering (Default)
By default, getBanners() automatically renders banners in the target element:
// Auto-render banners (default behavior)
await tappd.getBanners('#header-banner');
// Explicitly enable auto-render
await tappd.getBanners('#header-banner', { autoRender: true });The SDK will:
- Find the target element using the selector
- Create and style the banner element
- Add content (title, message, image, CTA button)
- Add close button if dismissible
- Apply animations (fade/slide)
- Track display event automatically
- Handle click and dismiss events automatically
Custom UI Rendering
If you want to render banners with your own UI, you can disable auto-render and use the returned banner data:
// Fetch banners without auto-rendering
const banners = await tappd.getBanners('#header-banner', { autoRender: false });
// Render with custom UI
banners.forEach(({ banner, variant }) => {
// Create your custom banner element
const customBanner = document.createElement('div');
customBanner.innerHTML = `
<h3>${variant.content.title}</h3>
<p>${variant.content.message}</p>
<button onclick="handleClick('${banner._id}')">${variant.content.cta.text}</button>
`;
// Track display manually
tappd.banners.display(banner._id, variant._id, '#header-banner')
.then(displayId => {
// Store displayId for click/dismiss tracking
customBanner.dataset.displayId = displayId;
});
document.querySelector('#header-banner').appendChild(customBanner);
});
// Handle click with manual tracking
async function handleClick(bannerId) {
const bannerEl = document.querySelector(`[data-banner-id="${bannerId}"]`);
const displayId = bannerEl.dataset.displayId;
await tappd.banners.click(bannerId, displayId);
// Handle CTA action...
}Manual Event Tracking
For custom UI implementations, you can manually track banner events:
Track Display
// Track banner display and get displayId
const displayId = await tappd.banners.display(
bannerId, // Banner ID
variantId, // Variant ID
selector // Placement selector (e.g., '#header-banner')
);Track Click
// Track banner click
await tappd.banners.click(bannerId, displayId);Track Dismiss
// Track banner dismiss
await tappd.banners.dismiss(bannerId, displayId);Banner Response Structure
The getBanners() method returns an array of banner objects:
[
{
banner: {
_id: "banner_id_123",
name: "Summer Sale Banner",
placement: {
type: "css_selector",
cssSelector: "#header-banner",
dataAttribute: null,
dataAttributeValue: null
}
},
variant: {
_id: "variant_id_456",
name: "Variant A",
content: {
title: "Summer Sale",
message: "Get 50% off on all items!",
imageUrl: "https://example.com/banner.jpg",
link: "https://example.com/sale",
cta: {
text: "Shop Now",
link: "https://example.com/sale",
action: "link"
}
},
design: {
position: "top",
layout: "horizontal",
backgroundColor: "#ffffff",
textColor: "#000000",
ctaBackgroundColor: "#007bff",
ctaTextColor: "#ffffff",
fontFamily: "Arial, sans-serif",
fontSize: "16px",
padding: "16px",
margin: "0",
borderRadius: "4px"
},
config: {
dismissible: true,
autoHide: false,
autoHideDelay: 5000,
animation: "fade",
closeButton: true
}
}
}
]Complete Examples
Example 1: Auto-Render on Page Load
import { TappdSDK } from '@tappd/web-sdk';
const tappd = new TappdSDK({
appId: 'YOUR_APP_ID',
apiUrl: 'https://sdk.gotappd.com/api/v1/sdk'
});
// Identify user
await tappd.identify({
external_id: 'user_123',
email: 'john@example.com',
name: 'John Doe'
});
// Auto-render banners when page loads
document.addEventListener('DOMContentLoaded', async () => {
// Render banners for header
await tappd.getBanners('#header-banner');
// Render banners for data attribute placement
await tappd.getBanners('data-tappd-banner="promo"');
});Example 2: Custom UI with Manual Tracking
import { TappdSDK } from '@tappd/web-sdk';
const tappd = new TappdSDK({
appId: 'YOUR_APP_ID',
apiUrl: 'https://sdk.gotappd.com/api/v1/sdk'
});
await tappd.identify({
external_id: 'user_123',
email: 'john@example.com'
});
// Fetch banners without auto-rendering
const banners = await tappd.getBanners('#header-banner', { autoRender: false });
// Render with custom UI
banners.forEach(async ({ banner, variant }) => {
const container = document.querySelector('#header-banner');
const bannerEl = document.createElement('div');
bannerEl.className = 'custom-banner';
bannerEl.innerHTML = `
<img src="${variant.content.imageUrl}" alt="${variant.content.title}">
<div>
<h3>${variant.content.title}</h3>
<p>${variant.content.message}</p>
<button class="cta-btn">${variant.content.cta.text}</button>
<button class="close-btn">×</button>
</div>
`;
// Track display
const displayId = await tappd.banners.display(banner._id, variant._id, '#header-banner');
bannerEl.dataset.displayId = displayId;
bannerEl.dataset.bannerId = banner._id;
// Handle CTA click
bannerEl.querySelector('.cta-btn').addEventListener('click', async () => {
await tappd.banners.click(banner._id, displayId);
window.location.href = variant.content.cta.link;
});
// Handle dismiss
bannerEl.querySelector('.close-btn').addEventListener('click', async () => {
await tappd.banners.dismiss(banner._id, displayId);
bannerEl.remove();
});
container.appendChild(bannerEl);
});Banner Features
Targeting
Banners support customer targeting:
- Segments: Target specific customer segments
- Rules: Custom targeting rules based on customer attributes
- Enabled/Disabled: Toggle targeting on/off
Banners with targeting enabled will only be returned if the customer matches the targeting criteria.
Scheduling
Banners support scheduling:
- Start/End Dates: Set when banners should be active
- Active Hours: Define specific hours when banners should display
- Active Days: Define specific days of the week
- Timezone: Set timezone for schedule calculations
Frequency Capping
Control how often banners are shown:
- Max Displays: Maximum number of times to show a banner
- Period: Time period (session, hour, day, week, month)
- Enabled/Disabled: Toggle frequency capping
A/B Testing
Banners support A/B testing with variants:
- Multiple Variants: Create multiple banner variants
- Traffic Allocation: Control percentage of traffic for each variant
- Experiment Assignment: Automatic variant assignment based on customer ID
Analytics
Banners track comprehensive analytics automatically:
- Views: Number of times banner was displayed (auto-tracked)
- Clicks: Number of times banner was clicked (auto-tracked)
- Dismissals: Number of times banner was dismissed (auto-tracked)
- Conversions: Number of conversions from banner
- CTR: Click-through rate (clicks / views)
Best Practices
- Identify Users Early: Call
identify()before fetching banners to enable targeting - Handle Missing Elements: SDK handles missing elements gracefully (logs warning)
- Auto-Tracking: SDK automatically tracks all interactions - no manual tracking needed for auto-rendered banners
- Custom UI: Use manual tracking methods only when implementing custom UI
- Error Handling: SDK handles API errors gracefully and logs warnings
- Multiple Banners: SDK supports multiple banners in the same placement
- Mobile Responsive: Ensure your target elements are responsive on mobile devices
Troubleshooting
Banners not displaying
- Check that user is identified (
identify()has been called) - Verify placement element exists in DOM
- Check browser console for warnings/errors
- Verify banner is active and published in dashboard
- Check targeting conditions are met
- Verify schedule is active
- Check frequency capping hasn't been reached
Banners displaying incorrectly
- Check CSS selector or data attribute matches
- Verify banner styles are applied correctly
- Check for CSS conflicts with your site styles
- Ensure container element has proper dimensions
Tracking not working
- Verify API endpoints are correct (check
apiUrlconfig) - Check that
displayIdis stored and passed correctly (for custom UI) - Check browser console for network errors
- Verify App ID is correct
API Reference
getBanners(selector, options?)
Fetch and optionally auto-render banners for a placement.
Parameters:
selector(string): CSS selector or data attribute selectoroptions(object, optional):autoRender(boolean): Auto-render banners (default:true)
Returns: Promise<Banner[]> - Array of banner data
banners.display(bannerId, variantId, selector)
Manually track banner display.
Parameters:
bannerId(string): Banner IDvariantId(string): Variant IDselector(string): Placement selector
Returns: Promise<string> - Display ID for use with click/dismiss
banners.click(bannerId, displayId)
Manually track banner click.
Parameters:
bannerId(string): Banner IDdisplayId(string): Display ID frombanners.display()
Returns: Promise<void>
banners.dismiss(bannerId, displayId)
Manually track banner dismiss.
Parameters:
bannerId(string): Banner IDdisplayId(string): Display ID frombanners.display()
Returns: Promise<void>
Related Documentation
- In-App Messages - Different from banners, these are trigger-based messages
- API Reference - Complete API documentation
- Configuration - All configuration options
- Examples - Code examples
