Pass the user agent to Hypertune

Overview

You may need to pass the user agent to Hypertune, e.g. to include it in analytics event payloads, target features or experiments, or provide personalization context to AI loops.

A common pattern is detecting crawlers or bots and returning a specific feature variant early. This can ensure they don't enter a Split, keeping exposures and analytics clean.

Setup

To pass the user agent to Hypertune, first add a userAgent field to your User input type:

input User {
  id: String!
  name: String!
  email: String!
  userAgent: String!
}

Then get the user agent from the user-agent header and pass it to Hypertune:

lib/getHypertune.ts
import "server-only";
import { unstable_noStore as noStore } from "next/cache";
import { headers } from "next/headers";
import { createSource } from "@/generated/hypertune";

const hypertuneSource = createSource({
  token: process.env.NEXT_PUBLIC_HYPERTUNE_TOKEN!,
});

export default async function getHypertune() {
  noStore();
  await hypertuneSource.initIfNeeded(); // Check for flag updates

  const headersList = await headers();
  const userAgent = headersList.get("user-agent") ?? "";

  return hypertuneSource.root({
    args: {
      context: {
        environment: process.env.NODE_ENV,
        user: { id: "1", name: "Test", email: "[email protected]", userAgent },
      },
    },
  });
}

Now you can use the user agent in:

  • Analytics event payloads

  • Targeting for features, experiments, or AI loops

Last updated