Skip to main content

Posthog

Sumatra can send events to Posthog analytics when a visitor is assigned to a targeted experience or experiment. This allows you to add the Sumatra experience as additional context when analyzing traffic and conversions in Posthog.

Impressions

To asynchronously send a sumatra_impression event on every page view, add the following code directly on your page, below the Sumatra snippet, or via Google Tag Manager.

<script>
window.sumatra.ready((sumatra) => {
sumatra.optimizer.subscribe((assignedVariants) => {
posthog.capture('sumatra_impression', { assignedVariants });
})
})
</script>

The resulting payload will look something like:

{
"assignedVariants": [
"signups-hero-default",
"signups-body-light"
]
}

User Actions

To add context to an event that a user initiates synchronously, you can access the sumatra.optimizer.assignedVariants variable directly.

Button click example:

<button id="clickme" onclick="trackClick(this.id)">Click Me</button>

<script>
function trackClick(id) {
posthog.capture('click', {
button: id,
assignedVariants: window.sumatra.optimizer.assignedVariants
});
}
</script>