Ghost ID Docs
Product documentation
Back to Dashboard

GHOST Namespace API Reference

Use the `GHOST` namespace, also available as `window["ghost-id"]`, to track custom activity, revenue, and identity updates from your site.

📡 Tracking Methods

GHOST.track(eventName, properties?)

JavaScript
GHOST.track("button_clicked", { section: "hero", variant: "A" });

GHOST.pageview()

JavaScript
// Usually auto-tracked. Call manually for custom SPA routing:
GHOST.pageview();

GHOST.revenue(amount, currency?, transactionId?, metadata?)

JavaScript
GHOST.revenue(99.99, "USD", "txn_123", { plan: "pro", billing: "annual" });

GHOST.error(error, properties?)

JavaScript
try { riskyOperation(); } catch(e) {
  GHOST.error(e, { context: "checkout", step: 3 });
}

GHOST.trackOutbound(url, text?, target?)

JavaScript
GHOST.trackOutbound("https://partner.com", "Visit Partner", "_blank");

🔑 Identity Methods

GHOST.identify(userId, traits?)

JavaScript
GHOST.identify("user@example.com", {
  name: "Jane Smith",
  company: "Acme Corp",
  plan: "enterprise"
});
  • Links the current visitor to a known identity.
  • All past anonymous activity is retroactively attributed.
  • Traits are stored and visible on the visitor profile.

GHOST.getVisitorId()

JavaScript
const visitorId = GHOST.getVisitorId();

Returns the current Ghost Visitor ID (GID).

GHOST.getUserId()

JavaScript
const userId = GHOST.getUserId();

Returns the identified user ID or `null` if the visitor is anonymous.

GHOST.clearUserId()

JavaScript
GHOST.clearUserId();

Removes the stored identity so the visitor becomes anonymous again.

GHOST.setTraits(traits)

JavaScript
GHOST.setTraits({ company: "Acme Corp", plan: "enterprise" });

Updates traits for an already-identified visitor.

âš™ī¸ HTML Data Attributes

Track events without writing JavaScript by adding Ghost data attributes directly to your HTML.

HTML
<button
  data-ghost-id-event="cta_clicked"
  data-ghost-id-prop-section="pricing"
  data-ghost-id-prop-plan="pro"
>
  Get Started
</button>
â„šī¸

Use HTML data attributes when you want simple event tracking without custom script handlers.