Table of Contents
JavaScript SDK
The JavaScript SDK from AdSelect enables developers to embed intelligent, contextual ads directly into their websites or chat UIs. It provides both low-level control (fetching JSON or HTML ad code) and high-level automation (rendering into elements or invoking callbacks).
All functions require authentication via:
- `apiId` — Your AdSelect project API ID
- `apiKey` — Your AdSelect API key (provided by support)
Installation
Simply include the SDK on your page:
<script src="http://147.93.43.199/js/adselect.js"></script>
The SDK will be available as `window.AdSelect`.
Basic Initialization
The SDK automatically initializes when loaded and exposes all functions on the global `AdSelect` object:
const AdSelect = window.AdSelect; console.log('SDK version:', AdSelect.version); console.log('Initialized at:', AdSelect.initializedAt);
Sending Chat History
Use `sendChatHistory(apiId, apiKey, userID, conversationID, messages)`.
Parameters
- `apiId`, `apiKey` — AdSelect API credentials
- `userID` — Unique user identifier
- `conversationID` — Chat session ID
- `messages` — Array of message objects:
- `role`: `“User”` or `“Bot”`
- `text`: Message string
Example
await AdSelect.sendChatHistory("YOUR_API_ID", "YOUR_API_KEY", "user123", "chat789", [ { role: "User", text: "What's the best laptop under $1000?" }, { role: "Bot", text: "I recommend models from Dell and Lenovo." } ]);
Getting Ad for Chat
Use `getChatAd(apiId, apiKey, userID, conversationID, adType, jsFunc)`.
Parameters
- `apiId`, `apiKey` — Your AdSelect credentials
- `userID`, `conversationID` — Chat context
- `adType` — One of the supported ad types (see below)
- `jsFunc` — Optional callback function or function name (for JavaScript ads)
Example
// Using async/await const adHtml = await AdSelect.getChatAd( "YOUR_API_ID", "YOUR_API_KEY", "user123", "chat789", AdSelect.AdTypeHtmlText ); document.getElementById("ad-slot").innerHTML = adHtml; // Using callback AdSelect.getChatAd( "YOUR_API_ID", "YOUR_API_KEY", "user123", "chat789", AdSelect.AdTypeJsonText, function(adData) { console.log("Received ad:", adData); } );
Getting Ad for a Web Page
Use `getPageAd(apiId, apiKey, pageContent, adType, callback)`.
Parameters
- `apiId`, `apiKey` — Your AdSelect credentials
- `pageContent` — Visible text or HTML source
- `adType` — One of the supported ad types
- `callback` — Optional callback function
Example
const pageContent = document.documentElement.innerText; // Using async/await const adHtml = await AdSelect.getPageAd( "YOUR_API_ID", "YOUR_API_KEY", pageContent, AdSelect.AdTypeHtmlText ); // Using callback AdSelect.getPageAd( "YOUR_API_ID", "YOUR_API_KEY", pageContent, AdSelect.AdTypeJsonText, function(adData) { console.log("Page ad:", adData); } );
Automatic HTML Rendering
For Page Ads
Use `renderPageAdIntoElement(apiId, apiKey, adType, elementId)`.
<div id="page-ad"></div> <script> AdSelect.renderPageAdIntoElement( "YOUR_API_ID", "YOUR_API_KEY", AdSelect.AdTypeHtmlText, "page-ad" ); </script>
For Chat Ads
Use `renderChatAdIntoElement(apiId, apiKey, userID, conversationID, adType, elementId)`.
<div id="chat-ad"></div> <script> AdSelect.renderChatAdIntoElement( "YOUR_API_ID", "YOUR_API_KEY", "user123", "chat789", AdSelect.AdTypeHtmlImage, "chat-ad" ); </script>
Supported Ad Types
The SDK provides constants for all supported ad types:
// JSON output formats AdSelect.AdTypeJsonText // Returns JSON for text ads AdSelect.AdTypeJsonImage // Returns JSON for image ads AdSelect.AdTypeBannerMediumRectJson // Returns JSON for Medium Rectangle banner ads AdSelect.AdTypeBannerLeaderboardJson // Returns JSON for Leaderboard banner ads AdSelect.AdTypeBannerWideSkyJson // Returns JSON for Wide Skyscraper banner ads // HTML output formats AdSelect.AdTypeHtmlText // Returns HTML for text ads AdSelect.AdTypeHtmlImage // Returns HTML for image ads AdSelect.AdTypeBannerMediumRectHtml // Returns HTML for Medium Rectangle banner ads AdSelect.AdTypeBannerLeaderboardHtml // Returns HTML for Leaderboard banner ads AdSelect.AdTypeBannerWideSkyHtml // Returns HTML for Wide Skyscraper banner ads
Error Handling
All async functions throw errors that should be caught:
try { const ad = await AdSelect.getChatAd(apiId, apiKey, userID, conversationID, adType); console.log("Success:", ad); } catch (error) { console.error("Error getting ad:", error.message); }
For render functions, errors are logged to console automatically:
// Errors are handled internally and logged AdSelect.renderPageAdIntoElement(apiId, apiKey, adType, "non-existent-element"); // Console: "renderPageAdIntoElement error: Element with id 'non-existent-element' not found"
