---
title: "Keep test and bot traffic out of the · Agent Wranglers"
url: https://agentwranglers.se/playbook/analytics/events-and-metrics/exclude-test-traffic
id: ANA-4
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 966
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Keep test and bot traffic out of the numbers

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Events and metrics](https://agentwranglers.se/playbook/analytics/events-and-metrics.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: ANA-4
- Level: Recommended
- Chapter: [Events and metrics](https://agentwranglers.se/playbook/analytics/events-and-metrics.md)
- Rule: Mark every event from staff, test accounts, automated tests and known bots at the collector, and leave marked events out of every metric.
- Check: Run the browser test suite against staging with the header. The collector stores its events with the flag set. A query over every metric view finds no row built from a flagged event.
- How: Flag test and staff accounts in the account table, and copy the flag onto each event on the server. Let browser tests and uptime checks send a header, such as `X-Test-Traffic: 1`, that the collector turns into the same flag. Mark known bots by user agent with a maintained list, such as the `isbot` package or the IAB bot list. Keep marked events in their own column, so you can still count them. Every shared metric view filters on that column. Write that filter into each metric as [Define each metric once](https://agentwranglers.se/playbook/analytics/events-and-metrics/metric-definitions.md) says.

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
import { isbot } from 'isbot';

export function markTraffic(event, request, account) {
  const isTest = request.headers['x-test-traffic'] === '1' || account?.isInternal === true;
  return { ...event, traffic: isTest ? 'test' : isbot(request.headers['user-agent'] ?? '') ? 'bot' : 'visitor' };
}
```

### Python example

```python
BOT_WORDS = ("bot", "crawler", "spider", "headless")

def mark_traffic(event: dict, headers: dict, account) -> dict:
    agent = headers.get("user-agent", "").lower()
    if headers.get("x-test-traffic") == "1" or (account and account.is_internal):
        traffic = "test"
    elif any(word in agent for word in BOT_WORDS):
        traffic = "bot"
    else:
        traffic = "visitor"
    return {**event, "traffic": traffic}
```

### SQL example

```sql
CREATE VIEW visitor_events AS
SELECT * FROM events WHERE traffic = 'visitor';
```

## Sources

- [Google Analytics known bot-traffic exclusion](https://support.google.com/analytics/answer/9888366)
- [Google Analytics internal traffic filter](https://support.google.com/analytics/answer/10104470)

## Related rules

- DEN-18 [Alert on sudden changes in volume](https://agentwranglers.se/playbook/data-engineering/data-quality-tests/volume-anomalies.md) in Data engineering
- ANA-5 [Define each metric once](https://agentwranglers.se/playbook/analytics/events-and-metrics/metric-definitions.md) in Product analytics and experiments

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/analytics/events-and-metrics/exclude-test-traffic">Agent Wranglers product analytics and experiments playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Record business facts on the server](https://agentwranglers.se/playbook/analytics/events-and-metrics/server-side-business-events.md)
- Next: [Define each metric once](https://agentwranglers.se/playbook/analytics/events-and-metrics/metric-definitions.md)
- [Events and metrics](https://agentwranglers.se/playbook/analytics/events-and-metrics.md)
