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.
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.
Click Molecule Pixel in the menu, or open Integrations and click Open on the Molecule Pixel tile.
Click Add a site and choose Custom code.
Give the site a name and list your Allowed domains, one per line.
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:
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:
Replace the three placeholders with the order's own values. For example, if your server prints them into the page:
Or, if the page already has them in a JavaScript variable:
Option 2: add it with Google Tag Manager
The code is the same as above, in two Custom HTML tags.
Tag 1: base code
Create a new tag of type Custom HTML.
Paste the base code from the site's card.
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:
For each value, go to Variables, click New and choose Data Layer Variable:
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
Create a new tag of type Custom HTML.
Paste this:
Set the trigger to a Custom Event with your purchase event's name, usually
purchase.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.
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
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
Load one page of your site.
Click Validate PageView on the site's card.
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:
Open your browser's developer tools on the Network tab and turn on Preserve log.
Place a test order, or open the order-confirmation page with a test order.
Filter for
batch. Within a few seconds you should see a request tohttps://t.moleculedata.com/api/s/batch.In the request's payload, find the event named
purchaseand checktransaction_id,valueandcurrency.
In Google Tag Manager, you can also check in Preview that the Purchase tag fired once and that its variables hold the right values.
Content-Security-Policy
If your site has a Content-Security-Policy, add this line. The site's card shows it too.
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:
__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
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.