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:
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`.
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);
Use `sendChatHistory(apiId, apiKey, userID, conversationID, messages)`.
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." } ]);
Use `getChatAd(apiId, apiKey, userID, conversationID, adType, jsFunc)`.
// 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); } );
Use `getPageAd(apiId, apiKey, pageContent, adType, callback)`.
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); } );
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>
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>
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
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"