> For the complete documentation index, see [llms.txt](https://docs.adpage.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.adpage.io/optimize-your-setup/profit-on-ad-spend/set-up-the-server-container.md).

# Set up the server container

Add the variables to your Google Tag Manager server container and check, in preview, that they carry real cost prices.

Two routes. The first works with either cost source and does the maths for you; the second is the older setup, still supported, feed only.

## Recommended: the AdPage Profit on Ad Spend variable

{% stepper %}
{% step %}

#### Create a lookup token

In your AdPage dashboard, go to **Webhook Matching → Settings → Send Profit on Ad Spend** and click **Create token**.

![The Lookup token on the Profit on Ad Spend card](https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2F2LRHVZcuZjXHhENWnFw9%2Fpoas-token.png?alt=media)

Each container has its own token and it unlocks that container's cost prices, so it belongs in your **server** container only. Never in a web container, never on your site.
{% endstep %}

{% step %}

#### Import the template

Download [adpage-profit-on-adspend.tpl](https://adpage.b-cdn.net/GTM-Templates/adpage-profit-on-adspend.tpl) — the same download sits next to the token in the dashboard.

In your server container, go to **Templates**, click **New** under Variable Templates, then the three dots in the top right → **Import**. Import the file and save.
{% endstep %}

{% step %}

#### Create the variable

Create a new variable with the imported template as its configuration. Two fields to fill in:

* **Container ID** — your AdPage container UUID, the same one as on the Event Notifier tag. Not the GTM container id.
* **Lookup token** — the token from step 1.

![The AdPage Profit on Ad Spend variable configuration](https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2FvBAEPgrGnNNkTokSieCu%2Fpoas-variable.png?alt=media)

Everything else has a working default. *Return* decides which field the variable gives back; *Where the number comes from* can stay on **Auto**.
{% endstep %}

{% step %}

#### Add it to your GA4 purchase tag

Add the variable as an event parameter on the tag you want the profit on. Use the parameter name that matches what you selected under *Return* — `profit_value` for net profit, `gross_profit` for gross.
{% endstep %}
{% endstepper %}

## Existing setup: total purchase price and profit margin

Still supported and unchanged. Requires an XML feed.

{% stepper %}
{% step %}

#### Add the total purchase price variable

1. Download this variable template: [template](https://drive.google.com/file/d/1ksyCJc4jv2W7ldNGOPinSHIfMhKPCYSE/view).
2. Open your GTM server container and go to **Templates**. Click **New** under Variable Templates.
3. Click the three dots in the top right, then click **Import**. Import the template you just downloaded and save it.
4. Create a new variable. Choose the variable template you just imported as the variable configuration and save this variable.
   {% endstep %}

{% step %}

#### Add the profit margin variable

1. Go to **Templates** and add a new Variable Template by searching the template gallery. Look for the **Number & String Operations** template and add it to your workspace.\ <br>

   <figure><img src="https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2Fknywtmg8vdlU9HC64qTK%2Fimage.png?alt=media&#x26;token=ea3b2b22-b650-49e9-97dd-7012f07696f4" alt=""><figcaption></figcaption></figure>
2. Create a new variable with **Number & String Operations** as the variable configuration. Set the operation type to **Calculate** and the operation to **Subtract**. Parameter 1 is the eCommerce value (event data variable for `value`) and Parameter 2 is your total purchase price variable from the previous step. Save this variable as `profit_margin`.\ <br>

   <figure><img src="https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2FbZMV2cWOpe0KvGL4Y39T%2Fimage.png?alt=media&#x26;token=15f2600f-88d5-4210-879e-41a5ff5c2ad1" alt=""><figcaption></figcaption></figure>

{% endstep %}

{% step %}

#### Add the variables to your GA4 (purchase) event tag

* `purchase_price`: value set to your total purchase price variable
* `profit_margin`: value set to your profit margin variable<br>

<figure><img src="https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2FX3G9T79Mdd1gDshatnpV%2Fimage.png?alt=media&#x26;token=b3f75e74-a9f0-4d3c-a3b3-975e9ed68bc1" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

## Validate in preview

Open your server container and turn on **preview mode**.

If you measure purchases through a webhook, replay one from the Webhook Logs. If you measure them from your web container through a GA4 event tag, complete a test purchase (this does not apply to Shopify).

**For the AdPage variable**, set *Return* to **all** for the duration of the test. You then see the whole object instead of a single number:

![The variable in preview, returning the full object](https://1274044937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA65KwL1NwewlJbtCXsXd%2Fuploads%2Fo5tIb3DkIcLn8ct8acBO%2Fpoas-preview.png?alt=media)

Look at **`coverage.matched`** before you look at the amount. It says how many order lines had a real cost price. If it is 0 while an amount still comes out, you are looking at the fallback margin rather than at your cost prices — an amount that looks right and is not. Set *Return* back afterwards.

**For the existing setup**, check that the `purchase_price` parameter is present on the **Event Data** tab and in the GA4 tag parameters.

{% hint style="info" %}
Nothing there? Work through [Troubleshooting](/optimize-your-setup/profit-on-ad-spend/troubleshooting.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.adpage.io/optimize-your-setup/profit-on-ad-spend/set-up-the-server-container.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.
