Skip to content
Implementation

Track AI agent traffic in PostHog with Marshy

Send Marshy verdicts to PostHog with replay and duplicate handling. Preserve session identifiers and connect classifications to customer outcomes.

Obaid Ahmed4 min read

Track AI agent traffic in PostHog by forwarding Marshy’s classification as a custom event with the original verdict, automation kind, and a separate Marshy session identifier. Handle early events and replay, deduplicate deliveries within the document, and keep PostHog’s own session identity intact.

The tested browser-side bridge below connects to an existing PostHog installation. Your application’s normal analytics and backend records supply task and conversion outcomes.

Define the event contract

Use a stable event name such as visitor_classified. Include the original verdict, kind, and Marshy session identifier. Keep the latter in a custom property; it is not PostHog's own session ID.

The PostHog session documentation describes its separate session identifier and get_session_id() method. Preserve both identifiers when correlating systems. Do not overwrite $session_id with an unrelated SDK's ID.

The underlying Marshy SDK uses resemble:verdict and ResembleAgentDetection.onVerdict. The callback API can replay an existing verdict. That matters when your analytics component mounts after classification has already occurred.

Forward each observation once

This JavaScript example supports event delivery during initialization and replay after the SDK becomes available. Deduplication is local to the document and bounded. It does not promise exactly-once ingestion across reloads or tabs.

'use client';
import { useEffect } from 'react';
import posthog from 'posthog-js';

const forwarded = new Set();

export default function MarshyAnalyticsBridge() {
  useEffect(() => {
    let active = true;
    let attached = false;
    let attempts = 0;

    function forward(detail) {
      if (!active || !detail) return;
      const { verdict, kind, sessionId } = detail;
      if (!['human', 'agent', 'review'].includes(verdict)) return;
      if (typeof sessionId !== 'string' || !sessionId) return;
      const key = JSON.stringify([sessionId, verdict, kind]);
      if (forwarded.has(key)) return;
      forwarded.add(key);
      if (forwarded.size > 200) {
        forwarded.delete(forwarded.values().next().value);
      }
      posthog.capture('visitor_classified', {
        marshy_session_id: sessionId,
        agent_verdict: verdict,
        agent_kind: kind,
      });
    }

    const onEvent = event => forward(event.detail);
    window.addEventListener('resemble:verdict', onEvent);

    function attachReplay() {
      const sdk = window.ResembleAgentDetection;
      if (attached || typeof sdk?.onVerdict !== 'function') return;
      attached = true;
      sdk.onVerdict(forward);
    }
    attachReplay();
    const timer = setInterval(() => {
      attachReplay();
      if (attached || ++attempts >= 100) clearInterval(timer);
    }, 100);

    return () => {
      active = false;
      clearInterval(timer);
      window.removeEventListener('resemble:verdict', onEvent);
    };
  }, []);
  return null;
}

The bridge requests replay once and otherwise keeps listening for verdict events. Its inactive callback ignores emissions after unmount. Initialize analytics before mounting it, and adapt the forwarding function to your consent and analytics readiness rules. If consent is granted later, mount the bridge then so replay can supply a result that already exists.

Review the underlying verdict documentation and PostHog custom event API when adapting this pattern. SDK event names are implementation details; label the resulting analytics integration Marshy.

Validate timing and failure cases

In a test environment, check the following cases in your analytics event view:

Case Expected observation
Bridge mounts before classification One forwarded classification
Bridge mounts after classification Existing verdict forwarded through replay
Event and replay deliver the same result One forwarding per deduplication key
Verdict is review Review stays review
No verdict arrives No invented human event
Account changes Normal account identification follows your authentication flow

The module-level set also helps avoid duplicate forwarding during development effect remounts. The SDK documents a verdict as final for a visit. Derive session reports from session identifiers rather than counting every forwarded event as a new session.

Build a report with honest denominators

Start with classification coverage and task completion by cohort. Keep review and absent results visible. Compare sessions with similar task intent and traffic source before explaining a difference in conversion.

Send minimal identifiers and classification fields. Form contents and customer secrets are unnecessary for this bridge. For high-impact decisions, a browser event remains advisory: your backend determines permissions, quotas, and operation success.

Use the agent traffic report template and the measurement guide to turn these observations into a team report. Explore Marshy to investigate the browser journey behind the event.

Sources checked October 8, 2026. This is a manual integration pattern, not a claim of a native PostHog connector or exactly-once delivery.

Keep reading
Analytics

How to track AI referral traffic in GA4

Find observable AI assistant referrals in GA4, preserve traffic-source scope, and add Marshy classifications without confusing referrals with browser agents.

5 min read

Peopleand Agents

See who's visiting your website.

Connect your website to see agent traffic, follow session journeys, and understand your customers' activity.