> For the complete documentation index, see [llms.txt](https://docs.bugbug.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.bugbug.io/troubleshooting/testing-websites-with-google-adsense.md).

# Testing websites with Google AdSense

Automated tests on pages that display Google AdSense ads can be treated as invalid traffic. Learn how to keep ads out of your BugBug test sessions and protect your AdSense account.

If your website displays ads from Google AdSense (or Google Ad Manager), every page your tests open also loads those ads. Google treats impressions and clicks generated by automated tools as **invalid traffic**, and the publisher (website owner) is responsible for all traffic on the site - even if it comes from their own tests.

## What is the risk?

* **Impressions only** - usually harmless. Google filters out automated impressions and doesn't pay for them. However, frequent scheduled runs that open many pages with ads from the same IPs increase the share of invalid traffic on your account, which can lead to an **ad serving limit**.
* **Clicks on ads** - the real danger. Even a few repeated clicks from the same source look like click fraud and can result in withheld earnings, ad serving limits, or in the worst case **account suspension**.

Tests rarely click ads on purpose. It usually happens by accident:

* an ad loads late and shifts the layout, so the click lands on the ad instead of the intended element,
* a selector is too generic (for example "the first link on the page") and matches a link inside an ad,
* an anchor or vignette (full-screen) ad from Auto ads covers the element the test wants to click.

{% hint style="info" %}
Ads injected by Auto ads also change the page layout between runs, which makes tests flaky. Disabling ads for test sessions protects your AdSense account **and** makes your tests more stable.
{% endhint %}

## How to prevent it

The safest approach is to **not load the AdSense script at all** during test runs. Your website needs to recognize BugBug traffic and skip the `adsbygoogle.js` script (and the `<ins class="adsbygoogle">` tags). Pick the option that fits your setup best.

{% hint style="warning" %}
All the options below except the first one require a small change on your website. Ask your developer to add a condition that skips loading AdSense when the marker from BugBug is present.
{% endhint %}

### Option 1: Test on a staging environment without ads

If you have a staging or development environment, keep AdSense disabled there and run most of your tests against it. Use [profiles and variables](https://docs.bugbug.io/editing-tests/variables.md#work-with-different-development-environments) to switch between environments. Run only a small set of smoke tests on production and protect them with one of the options below.

### Option 2: Send a custom HTTP header or user-agent (recommended for production)

1. Go to **Project settings > Browser**.
2. Add a header in **HTTP request headers** (for example `X-BugBug-Test` with a secret value of your choice), or set a custom **User agent**.
3. On your server (or in your tag manager), skip rendering the AdSense script when the request contains this header or user-agent.

This works for every test in the project, both local and cloud runs, with no changes to the tests themselves. Read more in [Project settings](https://docs.bugbug.io/preventing-failed-tests/project-settings.md).

### Option 3: Add a query string parameter

Add a parameter such as `?bugbug=1` to the URL in your **Go to URL** steps, and don't load AdSense when the parameter is present. If you use Google Tag Manager to load AdSense, you can add this as an exception to the tag's trigger.

Keep in mind that the parameter is only present on the first page. If the test navigates further, your website should remember it, for example in a cookie or `sessionStorage`.

### Option 4: Set a cookie or localStorage flag with Custom JavaScript

Add a [Custom JavaScript](https://docs.bugbug.io/editing-tests/custom-javascript-actions.md#javascript-steps) step at the beginning of the test that sets a flag, then reload the page:

```javascript
document.cookie = "disable_ads=1; path=/";
```

Your website then checks for this cookie before loading AdSense. To reuse the step across tests, put it in a [component](https://docs.bugbug.io/editing-tests/components.md).

### Option 5: Exclude BugBug cloud runner IPs

Cloud runs reach your site from the IP address of the Cloud Proxy selected in project settings. Your server can skip AdSense for requests coming from that address. See the [IPs list of cloud runners](https://docs.bugbug.io/troubleshooting/ips-list-of-cloud-runners.md). Remember that local runs come from your own IP address, so this option alone does not cover them.

### Option 6: Use AdSense test mode

If you want to keep ads visible in tests (for example to check the layout), add `data-adtest="on"` to your ad units during test sessions only. Ads in test mode are displayed but not counted or billed. Combine it with one of the options above to enable test mode only for BugBug traffic - never leave it on for regular visitors.

## Good practices in your tests

Even with ads disabled, follow these rules to avoid accidental clicks:

* **Use precise** [**selectors**](https://docs.bugbug.io/preventing-failed-tests/selectors.md) - avoid generic selectors such as "first link" or "first button" that could match an element inside an ad.
* **Never interact with ad iframes** - don't record or add steps inside iframes served by `googlesyndication.com` or `doubleclick.net`.
* **Wait for a stable layout** - use [waiting conditions](https://docs.bugbug.io/preventing-failed-tests/waiting-conditions.md) so the page stops shifting before the test clicks.
* **Watch for vignette and anchor ads** - if they appear, disable them for test sessions instead of adding steps that close them.

{% hint style="info" %}
The same approach applies to other third-party scripts that shouldn't run during tests, such as conversion tags, analytics or [A/B testing tools](https://docs.bugbug.io/troubleshooting/a-b-tests.md).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.bugbug.io/troubleshooting/testing-websites-with-google-adsense.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
