> For the complete documentation index, see [llms.txt](https://docs.moleculedata.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.moleculedata.com/tracking-ad-channels/website-tracking/custom-code.md).

# Install with custom code

Install Molecule's website tracker on any site that is not on Shopify, in your site's code or with Google Tag Manager.

Use this guide for any website that is not on Shopify. On Shopify, use the pixel described in [Molecule Pixel](https://docs.moleculedata.com/tracking-ad-channels/website-tracking.md).

You add two things:

* **Base code** on every page. It loads the tracker and sends a PageView.
* **A Purchase call** on the order-confirmation page. It sends the order's value, currency and id.

You can add them directly in your site's code, or as two Custom HTML tags in Google Tag Manager. Use one or the other.

## Before you start

The Molecule Pixel is part of the Pro and Enterprise plans. Only an admin can add a site.

1. Click **Molecule Pixel** in the menu, or open **Integrations** and click **Open** on the **Molecule Pixel** tile.
2. Click **Add a site** and choose **Custom code**.
3. Give the site a name and list your **Allowed domains**, one per line.
4. Click **Create site**, then open the site's card. It shows your code with your site key already in it.

Events from a domain that is not on your list are held back, so list every domain that loads the code:

| You enter                  | It covers                                                                                              |
| -------------------------- | ------------------------------------------------------------------------------------------------------ |
| `https://www.yourshop.com` | Only `www.yourshop.com`, not `yourshop.com`                                                            |
| `https://yourshop.com`     | Only `yourshop.com`, not `www.yourshop.com`                                                            |
| `https://*.yourshop.com`   | One level of subdomain, such as `www` or `shop`. Not `yourshop.com` itself, and not `a.b.yourshop.com` |

Domains must use `https`. Enter the domain only, without a path.

## Option 1: add it to your site's code

### Base code

Copy the base code from the site's card and paste it inside `<head>` on every page, including the order-confirmation page.

On a single-page site, each URL change sends one more PageView by itself. Don't add your own PageView calls.

### Purchase

On the order-confirmation page, call this once per order, after the base code:

```html
<script>
mlc('track', 'Purchase', { value: INSERT_VALUE, currency: 'INSERT_CURRENCY', order_id: 'INSERT_ORDER_ID' });
</script>
```

Replace the three placeholders with the order's own values. For example, if your server prints them into the page:

```html
<script>
mlc('track', 'Purchase', { value: 129.90, currency: 'EUR', order_id: 'A-1001' });
</script>
```

Or, if the page already has them in a JavaScript variable:

```html
<script>
mlc('track', 'Purchase', { value: order.total, currency: order.currency, order_id: order.id });
</script>
```

## Option 2: add it with Google Tag Manager

The code is the same as above, in two Custom HTML tags.

### Tag 1: base code

1. Create a new tag of type **Custom HTML**.
2. Paste the base code from the site's card.
3. Set the trigger to **Initialization - All Pages**. This makes sure the tracker exists before the Purchase tag fires.

### The Purchase variables

The Purchase tag needs a GTM variable for each value that changes per order. If your data layer follows the GA4 ecommerce format, it looks like this:

```javascript
dataLayer.push({
  event: 'purchase',
  ecommerce: { transaction_id: 'A-1001', value: 129.90, currency: 'EUR' }
});
```

For each value, go to **Variables**, click **New** and choose **Data Layer Variable**:

| Variable name              | Data Layer Variable Name   |
| -------------------------- | -------------------------- |
| `ecommerce.value`          | `ecommerce.value`          |
| `ecommerce.currency`       | `ecommerce.currency`       |
| `ecommerce.transaction_id` | `ecommerce.transaction_id` |

If your data layer uses other names, use those. Custom JavaScript and DOM Element variables work too.

### Tag 2: Purchase

1. Create a new tag of type **Custom HTML**.
2. Paste this:

```html
<script>
mlc('track', 'Purchase', { value: {{ecommerce.value}}, currency: {{ecommerce.currency}}, order_id: {{ecommerce.transaction_id}} });
</script>
```

3. Set the trigger to a **Custom Event** with your purchase event's name, usually `purchase`.
4. Test in **Preview**, then **Submit** and publish the container.

If your shop sells in one currency only, you can write it directly: `currency: 'EUR'`.

## When to use quotes

Quotes mark fixed text. A variable holds its own value, so it never takes quotes.

| What you put in                       | Quotes | Example                                  |
| ------------------------------------- | ------ | ---------------------------------------- |
| A GTM variable                        | No     | `order_id: {{ecommerce.transaction_id}}` |
| A JavaScript variable                 | No     | `order_id: order.id`                     |
| A number                              | No     | `value: 129.90`                          |
| Fixed text                            | Yes    | `currency: 'EUR'`                        |
| Text your server prints into the page | Yes    | `order_id: 'A-1001'`                     |

## The three values

Only these three values are sent. Anything else in the call is ignored.

| Value      | What to send                                                                                                                                         | If it's wrong                                                                                            |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| `value`    | The order's value as a number, in whole currency units with a dot: `129.90`, not `12990` or `129,90`                                                 | A value that isn't a number, such as `129,90`, is stored empty                                           |
| `currency` | The order's three-letter ISO 4217 code, such as `EUR` or `USD`. Set it per order if you sell in several currencies                                   | Molecule never assumes one. The Purchase is stored with no currency, and the site's card shows a warning |
| `order_id` | The same order id your orders carry in Molecule. For sales that come from GA4, that's the transaction id you send to GA4. Text or a number both work | The purchase can't be matched to its order. Molecule only matches on an exact id and never guesses       |

## Test it

### PageView

1. Load one page of your site.
2. Click **Validate PageView** on the site's card.
3. The card should say **Receiving**. Events show there within about a minute. Reports include them from the next day.

### Purchase

**Validate PageView** only checks PageViews, so test a Purchase in your browser:

1. Open your browser's developer tools on the **Network** tab and turn on **Preserve log**.
2. Place a test order, or open the order-confirmation page with a test order.
3. Filter for `batch`. Within a few seconds you should see a request to `https://t.moleculedata.com/api/s/batch`.
4. In the request's payload, find the event named `purchase` and check `transaction_id`, `value` and `currency`.

In Google Tag Manager, you can also check in **Preview** that the Purchase tag fired once and that its variables hold the right values.

{% hint style="info" %}
**Testing the same order twice?** The tracker sends each order id only once per session, so a reloaded thank-you page doesn't count twice. To test again, use a new order id or a new private window.
{% endhint %}

## Content-Security-Policy

If your site has a Content-Security-Policy, add this line. The site's card shows it too.

```
script-src-elem https://t.moleculedata.com; connect-src https://t.moleculedata.com
```

## Consent and what is stored

The custom code has no consent logic of its own. Like the Meta Pixel, it sends whenever your site's code calls it, so your site decides when it runs. If you need a visitor's consent first, load the base code and the Purchase call only after they give it. In Google Tag Manager, use the tags' consent settings or your consent tool's trigger.

The tracker sends each page's address and referrer, the visit's landing page and its referrer, ad click ids, an anonymous visitor id and session id, and the three Purchase values. It doesn't read your forms or your data layer. It also reads the `_fbc`, `_fbp`, `_ttp`, `_scid` and `_epik` ad cookies if they already exist.

It keeps these in the visitor's browser:

| Name          | Type                  | What it holds                                                           |
| ------------- | --------------------- | ----------------------------------------------------------------------- |
| `__eventn_id` | Cookie                | The anonymous visitor id                                                |
| `_mlc_ck`     | Cookie, up to 90 days | Ad click ids from the landing page. Some browsers keep it for less time |
| `mlc_s`       | Local storage         | The session, which ends after 30 minutes idle, and recent order ids     |
| `mlc_q`       | Session storage       | Events not sent yet                                                     |

## Troubleshooting

| What you see                                        | What to check                                                                                                                                                                    |
| --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Waiting**, or "No PageView yet"                   | Check that the base code is on the page you loaded and that the GTM container is published. Add the Content-Security-Policy line if you have a policy. Try without an ad blocker |
| **Check domains**                                   | The card names the domain. Add it to **Allowed domains** and click **Save domains**. It goes live within a few minutes                                                           |
| "A Purchase arrived without a currency"             | Pass the order's currency code in the Purchase call. In GTM, check in **Preview** that the currency variable holds a value                                                       |
| `mlc is not defined` in the browser console         | The Purchase call ran before the base code. Put the base code in `<head>`, or in GTM set Tag 1's trigger to **Initialization - All Pages**                                       |
| No `purchase` event in the Network tab              | That order id was already sent this session, the call has no values, or the tag didn't fire                                                                                      |
| A purchase on a page the visitor left straight away | A call made while the scripts are still loading, about a second on a first visit, is lost if the visitor leaves first. Call Purchase as early as you can                         |
| Nothing is sent from one page                       | A stray `%` in the page's address, such as `?q=100%`, stops the tracker on that page. Encode it as `%25`                                                                         |

Loading the base code twice does no harm: the second copy does nothing. If you **rotate your site key**, replace it in your code or in Tag 1. The old key keeps working for 7 days.
