Beacon.js

Get started with our Beacon.js Library!

🔆What is Beacon.js

beacon.js is a high-performance TypeScript library designed to track user interactions and analytics events for the Athos Commerce Beacon API. It provides real-time insights into user behavior across search, recommendations, autocomplete, and e-commerce journeys. This package can be used by both Athos Commerce and Searchspring accounts.

📘

Want a look under the hood? Check out the full Beacon.js build by navigating to our github

🚀 Key Features

  • Multi-Channel Tracking: Track events from search, autocomplete, recommendations, bundles, chat, category pages, and product pages.
  • Smart Batching: Automatically batches requests for optimal performance.
  • Local Storage Management: Manages user IDs, session IDs, cart state, viewed products, and recent search queries.
  • Attribution Tracking: Built-in support for tracking attribution and campaign sources.
  • Flexible Configuration: Support for custom headers, custom fetch implementations, and multiple environments.
  • Multi-Currency: Support for tracking transactions in different currencies.
  • Production Ready: Optimized for performance with features like keepalive requests and efficient batching.

🛠 Installation & Setup

The Beacon.js package can be used by both Athos Commerce and Searchspring accounts.

🚧

Using Snap? If you are currently on/building a Snap integration, please visit our Snap documentation site for further information on using Beacon.js. These instructions are intended for those building with theAthos APIs.

Option 1: CDN (Recommended for Rapid Integration)

📘

Note Utilizing this package via the CDN is preferred if you plan on integrating the Athos API and are not sending events directly to the beacon endpoint. This path is designed for platform template integrations such as BigCommerce, Magento, or Shopify.

Place the following script before the closing </head> tag. This makes the tracker available globally via window.athos.tracker.

<script siteId="YOUR_SITE_ID" src="https://cdn.athoscommerce.net/analytics/beacon.js"></script>

CDN initialization behavior:

  • A missing or unreadable siteId prevents initialization and logs an error.
  • If Snap tracking is already on the page, the script logs a warning and skips initialization.
  • If the script is included more than once, the second initialization is ignored with a warning.

Option 2: NPM (For API-driven Integrations)

Ideal for applications where you want full control over the lifecycle.

npm install --save @athoscommerce/beacon
import { Beacon } from '@athoscommerce/beacon';

// Initialize Beacon with required siteId
const beacon = new Beacon({ siteId: 'abc123' });

// Optionally set currency for transaction tracking
beacon.setCurrency({ code: 'USD' });

// Track an autocomplete render event
beacon.events.autocomplete.render({
  data: {
    // ... render data
  }
});

// Track a product page view
beacon.events.product.pageView({
  data: {
    result: {
      uid: 'variant-123',
      sku: 'SKU-123',
      parentId: 'product-123'
    }
  }
});
👍

Pro Tip If you are building a single-page application (React, Next.js, etc.), use the NPM package. This method allows you to initialize a single Beacon instance in a Global Context or Provider to update state automatically, ensuring your attribution data is never out of sync.


⚙️ Initialization

Setting up the Beacon involves two parts: identifying your siteId and (optionally) fine-tuning how the library communicates with the API.

  1. Basic Setup (Globals)

    The first argument is the Globals object. This is where you tell the library who you are.

import { Beacon } from '@athoscommerce/beacon';

const beacon = new Beacon({ 
  siteId: 'abc123' // Your Athos Site ID
});

Mappings

OptionTypeDescriptionRequired
siteIdstringYour unique Athos identifier.✔️
  1. Advanced Configuration

    The second parameter to the Beacon constructor provides optional configuration for API behavior and request handling.

const beacon = new Beacon(
  { siteId: 'abc123' },
  {
    mode: 'development',
    initiator: 'my-app/1.0.0',
    requesters: {
      beacon: {
        origin: 'https://custom-beacon.example.com/beacon/v2',
        headers: { 'Authorization': 'Bearer token' }
      },
      personalization: {
        origin: 'https://custom-personalization.example.com',
        headers: { 'X-Custom-Header': 'value' }
      }
    },
    apis: {
      fetch: customFetchImplementation
    },
    href: 'https://example.com/page',
    userAgent: 'Custom User Agent'
  }
);

Mappings

OptionTypeDescriptionDefault
mode'production' | 'development'Application mode. In development mode, errors are logged to console.'production'
initiatorstringIdentifier for the beacon instance.{athos|searchspring}/beaconjs/{version}
apis.fetchFetchAPICustom fetch implementation.Global fetch (if available)
requesters.beacon.originstringCustom beacon API endpoint.Athos: https://analytics.athoscommerce.net/beacon/v2 / Searchspring: https://analytics.searchspring.net/beacon/v2
requesters.beacon.headersHTTPHeadersCustom headers for beacon API requests.{ 'Content-Type': 'text/plain' }
requesters.personalization.originstringCustom personalization preflight origin (SDK appends /v1/preflight).https://{siteId}.a.{athoscommerce.net|searchspring.io}
requesters.personalization.headersHTTPHeadersCustom headers for personalization requests.{}
hrefstringOverride page URL for tracking.window.location.href
userAgentstringOverride user agent string.navigator.userAgent
🚧

Debugging: Set mode: 'development' to see detailed logs in your browser console without polluting your production analytics data.


🔑 Essential Tracking Properties

To ensure your analytics are accurate, pay close attention to these two properties. They connect the user's action back to the specific search or recommendation result.

  1. The responseId (Required)

    The responseId links events together. It is returned by the Search, Autocomplete, Category, and Recommendations APIs.

// Always extract this from your API response
const { responseId } = apiResponse;

window.athos.tracker.events.search.render({ 
  data: { responseId } 
});
  1. Merchandising Banner uid

    When building the data payload for banners, the uid property is located within the Banner content. Here is an example of how you may choose to extract it.

// Search API Example Response
const response = {
  "merchandising": {
    "content": {
      "header": [
            "<script data-banner-id=\"440998\" data-banner-type=\"html\" data-banner-html=\"<div style=&quot;width: 100%; background: #ADD8E6; text-align: center; padding: 20px;&quot;>On Sale</div>\" type=\"text/widget\"></script><div style=\"width: 100%; background: #ADD8E6; text-align: center; padding: 20px;\">On Sale</div>"
      ],
      "banner": [],
      "footer": [],
      "left": [],
      "inline": []
    }
  }
}
function getMerchandisingBannerUid(response, type) {
  // Extract data-banner-id from the HTML string
  const htmlString = response.merchandising?.content?.[type]?.[0] || '';
  const match = typeof htmlString === 'string' && htmlString.match(/data-banner-id="(\d+)"/);
  const uid = match ? match[1] : '';
  return uid;
}
window.athos.tracker.events.search.impression({ 
  data: {
    responseId: response.responseId,
    results: [],
    banners: [
      { uid: getMerchandisingBannerUid(response, 'header') } // { uid: '440998' }
    ]
  }
});
👍

Implementation Tip: Use a helper function to regex-match data-banner-id="(\d+)" from your merchandising content. This ensures you aren't manually hardcoding IDs that might change.


📊 Tracking Events

The Beacon class provides a comprehensive event tracking system organized by feature area. Each event method accepts a payload object containing the event data. An optional siteId can be provided to override the global siteId for a specific event.

📘

Note: Every event method follows a consistent structure:
tracker.events.[category].[action]({ data: { ... } }).

Event Categories & Actions

CategoryAvailable ActionsContext / Requirement
autocompleterender, impression, clickThrough, addToCart, redirectUsed in the search dropdown.
searchrender, impression, clickThrough, addToCart, redirectUsed on the main search results page.
categoryrender, impression, clickThrough, addToCartUsed on category listing pages (PLPs).
recommendationsrender, impression, clickThrough, addToCartRequires a tag (e.g., "homepage-recs").
bundlesrender, impression, clickThrough, addToCartRequires a tag (e.g., "pdp-bundles").
chatimpression, clickThrough, addToCart, feedbackRequires a chatSessionId for each event.
productpageViewUsed on Product Detail Pages (PDPs).
cartadd, removeUpdates internal cart state & local storage.
ordertransactionClears the cart and records the purchase.
shopperloginLinks a session to a Shopper ID.
errorshopifypixel, snapDiagnostic tracking for failures.

Quick View Interactions

A quick view is a popup or modal that previews a product without leaving the current page. When an interaction happens inside one, set the optional quickView flag to true in the event's data so reporting can attribute it separately from regular page interactions.

quickView is supported on the impression, clickThrough, and addToCart events of the autocomplete, search, category, recommendations, and bundles categories. Omit it (or set it to false) for interactions on a regular page.

// A click on a product inside a search quick view popup
window.athos.tracker.events.search.clickThrough({
  data: {
    responseId: response.responseId,
    quickView: true,
    results: [{ type: 'product', uid: 'variant-1', parentId: 'product-1', sku: 'SKU-1' }]
  }
});

📝Autocomplete Events

These events capture the user's journey within the search suggestion interface. All autocomplete interactions require the responseId from the API to maintain attribution.

The Workflow:

  • render: Fire this when the suggestion dropdown first appears.
  • impression: Fire this when products or banners become visible in the list.
  • clickThrough: Fire when a user selects a specific product or suggestion.
  • addToCart: Fire if a user adds to cart directly from a suggestion (this automatically updates the internal library cart state).
  • redirect: Fire if a suggestion triggers a hard redirect to a specific URL (e.g., a "Sale" page).

💻 Generic Implementation Structure:

// Generic structure for all Autocomplete actions
window.athos.tracker.events.autocomplete.[action]({
  data: {
    responseId: response.responseId,
    results: [{ type: 'product', uid: 'variant-1', parentId: 'product-1', sku: 'SKU-1' }], // For clicks, impressions, and cart adds
    banners: [{ uid: 'banner-123' }],       // For impressions
    redirect: 'https://example.com/sale'    // Only for .redirect()
  }
});
📘

Note: For a robust example of each Autocomplete Beacon event visit our Beacon.js Github


🔍 Search Events

Search events track how users interact with the main results page. Like Autocomplete, these rely heavily on the responseId to tie user behavior back to the specific search query performed.

The Workflow:

  • render: Fire when the search results page or results grid finishes loading.
  • impression: Fire as products or merchandising banners scroll into the user's view.
  • clickThrough: Fire when a user clicks a product to view its details.
  • addToCart: Fire when a user adds a product directly from the search grid.
  • redirect: Fire if the search query triggers a merchant-defined redirect.

💻 Generic Implementation Structure:

// All Search actions use this consistent structure:
window.athos.tracker.events.search.[action]({
  data: {
    responseId: '607bafd1-f624-4e58-afa5-b8b8e90929f5',
    results: [{ type: 'product', uid: 'variant-1', parentId: 'product-1', sku: 'SKU-1', price: 29.99 }], // For clicks, impressions, and cart
    banners: [{ uid: 'banner-123' }],                    // For impressions
    redirect: 'https://example.com/promo'                // Only for .redirect()
  }
});
📘

Note: For a robust example of each Search Beacon event visit our Beacon.js Github


📑 Category Events

Category events track user interactions on category or listing pages. These events rely on the responseId to link user behavior back to the specific category data returned by the system.

The Workflow:

  • render: Fire when the category page or results grid finishes loading.
  • impression: Fire as products or merchandising banners scroll into the user's view.
  • clickThrough: Fire when a user clicks a product from the listing to view its details.
  • addToCart: Fire when a user adds a product directly from the category grid.

💻 Generic Implementation Structure:

// All Category actions use this consistent structure:
window.athos.tracker.events.category.[action]({ 
  data: {
    responseId: '50c7aaf3-1909-43cd-8fff-a8e5c4452ddb',
    results: [
      { 
        type: 'product', 
        uid: 'product-1', 
        parentId: 'parent-1', 
        sku: 'SKU-1',
        qty: 1,      // Included for addToCart
        price: 49.99 // Included for addToCart
      }
    ],
    banners: [] // Used in impression events
  }
});
📘

Note: For a robust example of each Category Beacon event visit our Beacon.js Github


💡 Recommendations Events

Recommendations events track user interactions with personalized product carousels or recommendation widgets. These utilize a tag (to identify the specific widget location, like 'homepage') and a responseId to tie the interaction back to the recommendation engine's output.

The Workflow:

  • render: Fire when the recommendation widget finishes loading on the page.
  • impression: Fire when specific recommended products or banners scroll into the user's viewport.
  • clickThrough: Fire when a user clicks a recommended product to view its details.
  • addToCart: Fire when a user adds a product to their cart directly from the recommendation widget.

💻 Generic Implementation Structure:

// All Recommendations actions use this consistent structure:
window.athos.tracker.events.recommendations.[action]({ 
  data: {
    tag: 'homepage-recommendations',
    responseId: '1a304980-27d4-4f4b-96cc-758b280dfa7a',
    results: [
      { 
        type: 'product', 
        uid: 'product-1', 
        parentId: 'parent-1', 
        sku: 'SKU-1',
        qty: 1,      // Included for addToCart
        price: 39.99 // Included for addToCart
      }
    ],
    banners: [] // Used in impression events, not currently supported in recommendations
  }
});
📘

Note: For a robust example of each Recommendation Beacon event visit our Beacon.js Github


🗂 Bundles Events

Bundles events track user interactions with personalized product bundles. Like recommendations, they use a tag to identify the bundle profile and a responseId from the Personalized Bundles API.

The Workflow:

  • render: Fire when a bundle set is requested and rendered on the page.
  • impression: Fire when bundle products or banners scroll into the user's viewport.
  • clickThrough: Fire when a user clicks a bundled product and is taken to the PDP.
  • addToCart: Fire when a user adds a bundled product via Quick Add to Cart (omit if Quick Add is not implemented).

💻 Generic Implementation Structure:

window.athos.tracker.events.bundles.[action]({
  data: {
    tag: 'pdp-bundles',
    responseId: response.responseId,
    results: [
      {
        type: 'product',
        uid: 'variant-1',
        parentId: 'product-1',
        sku: 'SKU-1',
        qty: 1,      // Included for addToCart
        price: 39.99 // Included for addToCart
      }
    ],
    banners: [] // Used in impression events
  }
});
📘

Note: For a robust example of each Bundles Beacon event visit our Beacon.js Github


💬 Chat Events

Chat events track user interactions with products surfaced in a Conversational Assistant chat session. Every chat event requires a chatSessionId to associate it with a specific shopper conversation. See the Chat Tracking Guide for when to send each event.

The Workflow:

  • impression: Fire when products surfaced by chat become visible.
  • clickThrough: Fire when a user clicks a product from chat results.
  • addToCart: Fire when a user adds a product to cart from chat results.
  • feedback: Fire to record positive or negative shopper feedback on the chat session.

💻 Generic Implementation Structure:

// impression, clickThrough, and addToCart
window.athos.tracker.events.chat.[action]({
  data: {
    chatSessionId: 'chat-session-12345',
    responseId: response.responseId,
    results: [
      {
        type: 'product',
        uid: 'variant-1',
        parentId: 'product-1',
        sku: 'SKU-1',
        qty: 1,      // Included for addToCart
        price: 29.99 // Included for addToCart
      }
    ]
  }
});

// feedback
window.athos.tracker.events.chat.feedback({
  data: {
    chatSessionId: 'chat-session-12345',
    feedback: 'positive' // or 'negative'
  }
});
📘

Note: For a robust example of each Chat Beacon event visit our Beacon.js Github


🧑‍💼 Shopper Events

Shopper events link an anonymous session to a known customer account.

The Workflow:

  • login: Fire when a user logs into their shopper account. This triggers a login event to the beacon and sends a preflight request for personalization.

💻 Generic Implementation Structure:

window.athos.tracker.events.shopper.login({
  data: { id: 'shopper-12345' }
});
👍

Pro Tip: Use beacon.setShopperId('shopper-12345') as an alternative — it sets the shopper ID, triggers the login event, and sends preflight in a single call.


📦 Product Events

Product events track how users interact with individual product detail pages (PDP). Unlike search or category events, these focus on a single product entity and are essential for updating the user's "recently viewed" history.

The Workflow:

  • pageView: Fire immediately when a product detail page finishes loading. This is the primary signal for tracking product interest and automatically updates the user's "recently viewed" history.
📘

uid and parentId: For parent/variant catalogs, use parentId as the parent product ID and uid as the variant ID. For simple products with no variants, set uid and parentId to the same value. Include sku whenever it is available from your API response.

💻 Generic Implementation Structure:

// Product page views use this specific structure:
window.athos.tracker.events.product.pageView({ 
  data: {
    result: {
      uid: 'variant-123',     // variant ID (or product ID if no variants)
      parentId: 'product-123', // parent product ID (same as uid if no variants)
      sku: 'SKU-123'
    }
  }
});
📘

Duplicate page views: pageView is sent once per unique variant (parentId, uid, sku) per page load and URL path. Calling it again for a variant already tracked on the current page, such as when a shopper toggles back to a variant, does not send a duplicate. Switching to a different variant sends a new page view. Navigating to a new path or calling pageLoad() resets this, so single-page applications should call pageLoad() on every navigation, especially with hash-based routing.


🛒 Cart Events

Cart events track how users manage items in their shopping basket. These events are critical for calculating conversion rates and abandoned cart recovery, as the system automatically synchronizes the cart state with local storage.

The Workflow:

  • add: Fire whenever a product is added to the cart (from a PDP, quick-view, or grid).
  • remove: Fire when an item is deleted from the cart or the quantity is reduced to zero.

💻 Generic Implementation Structure:

// All Cart actions use this consistent structure:
window.athos.tracker.events.cart.[action]({ 
  data: {
    results: [
      { 
        uid: 'product-1', 
        parentId: 'parent-1',
        sku: 'SKU-1', 
        qty: 1, 
        price: 29.99 
      }
    ],
    // The 'cart' array should represent the full state of the basket AFTER the action
    cart: [
      { 
        uid: 'product-2', 
        parentId: 'parent-2',
        sku: 'SKU-2', 
        qty: 2, 
        price: 19.99 
      }
    ]
  }
});
📘

Note: For a robust example of each Cart Beacon event visit our Beacon.js Github


🛍️ Order Events

Order events track the final conversion step in the user journey. These events capture critical financial and geographic data, and firing a transaction automatically clears the stored cart state to prepare for the user's next session.

The Workflow:

  • transaction: Fire once the user successfully completes a purchase, typically on the order confirmation page.

💻 Generic Implementation Structure:

// Order transactions use this comprehensive structure:
window.athos.tracker.events.order.transaction({ 
  data: {
    orderId: 'order-12345',
    transactionTotal: 119.97, // Total price of items
    total: 129.97,            // Final total including tax/shipping
    vat: 0.20,
    city: 'New York',
    state: 'NY',
    country: 'US',
    results: [
      { 
        uid: 'product-1', 
        parentId: 'parent-1',
        sku: 'SKU-1', 
        qty: 2, 
        price: 29.99 
      },
      { 
        uid: 'product-2', 
        parentId: 'parent-2',
        sku: 'SKU-2', 
        qty: 1, 
        price: 60.00 
      }
    ]
  }
});

⚠️ Error Tracking Events

Error tracking events are vital for monitoring the health of your integration. They capture failures within specific implementation layers, such as Shopify Pixels or SNAP, allowing developers to debug issues with real-time context from the user's session.

🚧

Note: If you are not using Shopify Pixel or have an integrated SNAP layer in your project, you can skip the following workflow

The Workflow:

  • shopifypixel: Fire when an error occurs specifically within the Shopify Pixel sandbox or during data mapping for Shopify-specific events.
  • snap: Fire when errors occur within SNAP (Search, Navigation, and Personalization) implementations, such as failed API calls or rendering timeouts.

💻 Generic Implementation Structure:

// All Error actions use this consistent structure:
window.athos.tracker.events.error.[action]({ 
  data: {
    message: 'Detailed error description',
    stack: 'The stack trace or specific line of failure',
    details: { 
      // Contextual metadata relevant to the error
      productId: 'abc-123',
      tag: 'homepage-recs',
      endpoint: '/api/v1/endpoint'
    }
  }
});
📘

Note: For a robust example of each Error Beacon event visit our Beacon.js Github


💾 Storage & Context Management

Beacon.js automatically manages local storage and cookies to maintain user state across sessions. This powers attribution, "Recently Viewed" lists, and persistent carts without extra code.

  • User IDs: Persisted for 18 months.
  • Session IDs: Persisted for 30 minutes.
  • Cart: Current items in the user's cart
  • Viewed Products: Recently viewed products (up to 20 items, one per parent product, keeping the most recently viewed variant)
  • Searched Queries: Recent search queries for each siteId (up to 10, most recent first)
  • Attribution: Campaign/attribution source tracking

Manual Storage Control

While management is automatic, you can manually interact with these stores if your site has custom requirements (e.g., a "Clear My History" button).

// Accessing the Cart Store
const cart = beacon.storage.cart.get(); 
beacon.storage.cart.clear();

// Accessing Viewed Products
const history = beacon.storage.viewed.get();
beacon.storage.viewed.add([{ uid: 'variant-2', parentId: 'product-2', sku: 'SKU-2' }]);

// Accessing Searched Queries
beacon.storage.searched.add(['boots']);
const searched = beacon.storage.searched.get();
// Returns: [{ query: 'boots', timestamp: '2026-09-23T14:00:00.000Z' }, ...]

Searched Queries

Search events don't carry the query, so Beacon.js does not record searched queries on its own. Call beacon.storage.searched.add([query]) on each search (Snap does this automatically). Without it, lastSearches is never sent to personalization.

  • Queries are de-duplicated (case insensitive), and a repeated query moves to the front with a new timestamp.
  • Each siteId has its own list, stored under athosSearchedQueries-<siteId>. Every storage.searched method accepts an optional siteId as its last argument, defaulting to the beacon's siteId.
  • getLastSearches() returns the queries from the past 2 weeks. This is what preflight sends as lastSearches, and a change to it triggers a new preflight.
📘

Note: For more in depth examples of how to access and manipulate the tracked cart, viewed products, and searched queries visit our Beacon.js Github


🛠️ Developer Toolkit

The Beacon instance provides several utility methods to manage user identity, tracking context, and session state.

Reference Chart

MethodTypeDescription
setShopperId(id)ActionLinks a session to a specific customer ID. Triggers a login event and refreshes personalization.
pageLoad()ActionRequired for SPAs. Generates a new Page Load ID and resets product page view deduplication. Call this method on page transitions or navigation within a single-page application.
setCurrency({code})ActionSets the ISO 4217 currency code (e.g., 'USD', 'EUR') for all financial events.
updateContext(k, v)ActionDynamically updates context properties like pageUrl or dev flags. Updating pageUrl does not start a new page load, so also call pageLoad() on navigation.
sendPreflight()ActionSends preflight data to the personalization API. Automatically called when cart, viewed products, last searches, or shopper state changes, but can be manually triggered if needed. Accepts optional overrides for userId, siteId, shopper, cart, lastViewed, and lastSearches.
getContext()InfoGets the current tracking context including user, session, and page information.
getUserId()InfoReturns the persistent 18-month UUID for the visitor.
getSessionId()InfoReturns the current 30-minute session ID.
getShopperId()InfoReturns the current shopper ID if one has been set.
getPageLoadId()InfoReturns the current page load ID. Generate a new one with pageLoad().
getLastSearches()InfoReturns the searched queries from the past 2 weeks, most recent first. This is what preflight sends as lastSearches.
getTimestamp()InfoReturns the current timestamp in ISO 8601 format.
generateId()InfoGenerates and returns a new UUID.
👍

Want more Examples? View the full list of our Beacon.js public API methods, with use case examples at our Beacon.js Github


🔗 Attribution Tracking

Attribution data is automatically captured from the athos_attribution URL parameter and included in every tracking event's context. No manual configuration is required.

https://example.com/products?athos_attribution=email:campaign-123

The value format is channel:campaign-id. Use beacon.getContext() to inspect the current attribution state.


🏁 Wrapping Up

You now have the core building blocks for a full Athos Commerce Beacon.js integration. By implementing the event workflows above, you've ensured that search behavior, product interest, and conversion data are all communicating seamlessly with the Athos API!

Need to go deeper?

While the default setup handles the vast majority of e-commerce environments, Beacon.js is built to be flexible. If your project requires custom fetch implementations, API proxying, or complex environment overrides, we have you covered.

You can find documentation for these edge cases, along with full TypeScript definitions and low-level utility methods, in our Beacon.js GitHub Repository.

👍

Want more Examples? You can find documentation for these edge cases, along with full TypeScript definitions and low-level utility methods, in our Beacon.js GitHub Repository.

🥳

Happy coding! If you run into issues, remember to enable mode: 'development' to see real-time debugging logs in your console.


Did this page help you?