LemuAIDocs

Settings · 5 min read

Website and JavaScript

Add Lemu to any website with the snippet, or to a JavaScript app with the @lemuai/analytics package: page views, events, purchases, known users and consent

Open in ChatGPTOpen in ClaudeMarkdown

The same script powers the website snippet and the @lemuai/analytics npm package. It has no dependencies, is about 5.5 KB gzipped, and is safe to import during server-side rendering.

  • Use the snippet on any website, including Shopify themes, WordPress and site builders.
  • Use the package in a JavaScript or TypeScript app built with a bundler.
  • For React and Next.js, use @lemuai/react, which is built on this package.

You need the write key of a Website source. See Get a write key.

Add the snippet#

Copy the snippet from your source (Settings → Tracking Sources, the Any website tab under Set up) and paste it into the <head> of every page. It loads the script, starts it with your key and records a page view:

HTML
<script>
  !function(w,d){w.lemu=w.lemu||function(){(w.lemu.q=w.lemu.q||[]).push(arguments)};
  var s=d.createElement("script");s.async=1;s.src="https://app.lemuai.com/api/v1/pixel/sdk.js";
  d.head.appendChild(s)}(window,document);
  lemu("init", "YOUR_WRITE_KEY");
  lemu("page");
</script>

With the snippet, call every method by name, for example lemu("track", "AddToCart", { value: 999, currency: "INR" }). Calls made before the script has loaded are kept and run once it arrives.

Or install the package#

Shell
npm install @lemuai/analytics
JavaScript
import { lemu, LemuEvent } from "@lemuai/analytics";
 
lemu.init("YOUR_WRITE_KEY");
lemu.page();
 
lemu.track(LemuEvent.AddToCart, {
  value: 999,
  currency: "INR",
  content_ids: ["SKU1"],
  content_type: "product",
});

Calls made before init are kept and run after it. The package sends to https://app.lemuai.com unless you set apiHost.

Send events#

Use Meta's standard event names for standard actions, such as ViewContent, AddToCart and InitiateCheckout. With the package, use the LemuEvent constants. Any other name works as a custom event.

JavaScript
lemu("track", "ViewContent", { content_ids: ["SKU1"], content_type: "product", value: 999, currency: "INR" });
lemu("track", "coupon_applied", { code: "DIWALI10" });

value is in major units (999 is ₹999) and currency is an ISO 4217 code such as INR. See the event reference for every name and parameter.

Keep contact details such as email, phone and address out of event properties. Lemu removes them. Send them with identify instead.

Purchases#

Send every order with purchase on your thank-you or order confirmation page:

JavaScript
lemu("purchase", {
  value: 1499.5,
  currency: "INR",
  order_id: "1042",
  contents: [{ id: "SKU1", quantity: 1, item_price: 1499.5 }],
});

purchase sends a Purchase event at once, without waiting for the next batch, so it isn't lost if the visitor closes the tab.

value, currency and order_id are required. Lemu counts one conversion per order_id, so the same order reported by this page, a reload of the page, Shopify or your server counts once. Lemu never rejects an event for a missing field, but it flags the conversion. See Conversion events.

Identify visitors#

Call identify when the visitor signs up, logs in or fills a form, with your own user id and their contact details:

JavaScript
lemu.identify("crm-123", {
  email: "asha@example.com",
  phone: "+919876543210",
  first_name: "Asha",
});
 
// without your own id
lemu.identify({ email: "asha@example.com" });
 
// on logout
lemu.reset();

With the snippet, the same calls are lemu("identify", "crm-123", { ... }) and lemu("reset").

The user id is kept and sent with every later event until reset, which also gives the visitor a new anonymous id. See Identify visitors for how Lemu matches contacts.

Page views and single-page apps#

page(name?, properties?) records a page view with the page's URL, path, title and referrer. It's stored as PageView.

In a single-page app, start with trackPageChanges to record a page view on every history.pushState and on back and forward navigation:

JavaScript
lemu.init("YOUR_WRITE_KEY", { trackPageChanges: true });

With the snippet, pass options as the third argument: lemu("init", "YOUR_WRITE_KEY", { trackPageChanges: true }).

UTM parameters (utm_source, utm_medium, utm_campaign, utm_term, utm_content) and gclid / fbclid on the landing page are kept with the visitor's events. Personal data in page URLs, such as ?email=, is removed.

If you need consent before tracking, for example after your cookie banner, start with requireConsent:

JavaScript
lemu.init("YOUR_WRITE_KEY", { requireConsent: true });
 
// later, from your consent banner
lemu.setConsent(true); // snippet: lemu("consent", true)
  • Until consent, nothing is sent and nothing is written to cookies or local storage, not even the visitor id. Up to 50 events wait in memory and are sent once consent is given.
  • setConsent(false) discards them. A granted consent is remembered in local storage (_lemu_consent), so returning visitors aren't asked again. setConsent(false) forgets it.
  • optOut() stops all tracking and clears the queue. It is remembered in a cookie and in local storage. optIn() undoes it.

Debug mode#

JavaScript
lemu.init("YOUR_WRITE_KEY", { debug: true });

Every call, request and response is logged to the browser console with a [lemu] prefix. Bad arguments, such as an empty event name or properties that can't be turned into JSON, are logged and ignored. The script never throws into your code.

If the console shows a refused request, check that your site is listed under Allowed sites on the source.

Options#

OptionDefaultWhat it does
apiHosthttps://app.lemuai.comWhere to send events. The snippet sends to the host it was loaded from
debugfalseLogs calls, requests and responses
requireConsentfalseHolds events until setConsent(true)
trackPageChangesfalseRecords a page view on single-page-app navigation
cookieDomainthe current hostShares the visitor id across subdomains, for example .example.com
flushAt10Sends when this many events are queued
flushInterval1000Sends queued events after this many milliseconds
maxQueueSize1000The most events kept while offline

API#

MethodWhat it does
init(writeKey, options?)Starts tracking. Calls made before init are kept and run after it
page(name?, properties?)Records a page view
track(event, properties?)Sends any event
purchase({ value, currency, order_id, ... })Sends a Purchase at once
identify(userId?, traits?)Sets the user id and sends contact details
reset()New anonymous id, user id cleared
flush()Sends queued events now. Returns a promise
setConsent(granted)Starts or stops sending under requireConsent
optOut(), optIn()Stops or resumes all tracking, remembered in the browser
anonymousId()The visitor id, or null before consent or after opt-out
createLemu()A separate client, for sending to a second source from the same page (package only)

How events are sent#

Events are batched and sent every second or every 10 events, and with sendBeacon when the page is hidden, so events from the last page aren't lost. Requests are sent as plain text, so the browser makes no extra CORS request first. Failed requests are retried with increasing waits.

The visitor id lives in the _lemu_aid first-party cookie and in local storage, so returning visitors keep the same id.

Was this page helpful?