Frontend IntegrationWeb ObjectIntegration guide

Integration guide

How to deliver @usesophi/sophi-web-object to your storefront — choose between GTM, npm/bundler, or CDN script tag depending on your stack.

Choose how you deliver @usesophi/sophi-web-object. For Google Tag Manager, the detailed walkthrough is on a dedicated page so it is easy to find: GTM integration.

Chat widget

To load the Sophi assistant widget (SophiWidget, @usesophi/sophi-web-sdk) from GTM, use SDK GTM integration, not this package.

Choose an integration method

MethodBest whenUpgrade story
GTMMarketing owns tag publishing; consent-gated load; you want one place to bump the script URLPin SDK version in a GTM Constant variable; change it + publish container
Script tagFull control in your HTML templates; no GTMChange the src URL (or your CMS snippet) and deploy
npm / ESMBundled app; type-aware codebase; tree-shaking not required for this global SDKBump dependency in package.json and release your app

All methods expose window.sophi_object and, after bootstrap, window.SophiTracker (see SophiTracker API).

Script tag

Define window.sophi_object with at least config.apiKey, then load the script after that block (or ensure config exists before the tracker runs).

<script>
  window.sophi_object = {
    config: {
      apiKey: 'YOUR_API_KEY'
    },
    page: {
      type: 'Home',
      url: 'https://example.com/'
    }
  };
</script>
<script src="https://cdn.jsdelivr.net/npm/@usesophi/sophi-web-object@latest/dist/sophi.min.js" async></script>

async is supported: the SDK waits for DOMContentLoaded (or runs immediately if the document is already loaded) before bootstrapping.

CDN URLs and build artifacts are listed on Installation.

npm / ESM

npm install @usesophi/sophi-web-object

ES module import is hoisted, so assign window.sophi_object first, then load the SDK with dynamic import (or a small separate entry file that runs before your main bundle):

window.sophi_object = {
  config: { apiKey: 'YOUR_API_KEY' },
};

await import('@usesophi/sophi-web-object/dist/sophi.esm.js');
// After bootstrap: window.SophiTracker and window.sophi_object

Alternatively, set window.sophi_object in an inline <script> before your bundled app, or use the script tag / GTM approaches.

Best practices

  • Pin versions in production — Use an exact semver in URLs or package.json, not @latest, so releases do not change behaviour unexpectedly.
  • Consent — Load the tracker and populate PII only when your policy allows (see Configuration and Types for UserObject).
  • SPAs — After route or context changes, update via SophiTracker.push() or assignments; see SophiTracker API and Integration patterns.
  • Config and apiKey — The SDK may strip apiKey from the public object after startup; avoid rewriting it on every render if your integration relies on that behaviour (see Configuration).

Events cheat sheet

When you assign to watched keys without overriding event_type, defaults are:

KeyDefault event_type
pagepage_view
useridentify
productproduct_view
basketbasket_view
listinglisting_view
transactionpurchase

For add_to_cart, remove_from_cart, and checkout_started, set event_type explicitly (usually via SophiTracker.push). Details: Event types.

Next steps

On this page