17. Tracking and custom code
In BuilderOS → Tracking (top bar) the site is connected with measurement and advertising tools. The code a provider asks for goes in BuilderOS → Home → Custom code (?tab=code). No third-party plugin is needed and you don’t have to touch the theme.
Tracking navigation. First level, large and with an icon: TrackAssist (default; second level Overview, empty view, Connections, view=conexiones, and Events, view=trackassist; without TrackAssist it opens on Connections), ServerTrack, Analytics and Cookies (view=cookies, with the views General, Texts, Categories and Log; see “Cookie banner and consent mode v2”). The previous addresses still work; tab=tracking&view=code, tab=custom-code (Builder_OS_Admin::MOVED_TABS) and the anchors #bos-code, #bos-custom-code, #bos-tracking-code, #codigo and #code of Tracking lead to tab=code.
How it works
Tracking y Código personalizado En cada página pública (todos los idiomas)
──────────────────── ──────────────────────────────────────────
Identificadores (GTM-…, G-…) ──▶ <head> consentimiento → GTM → gtag → píxeles → tu código
Código personalizado ──▶ <body> noscript de GTM → tu código
Opciones ──▶ </body> eventos de los formularios → tu código- You only paste the identifier. BuilderOS writes each tool’s official code in the right place. An identifier with an incorrect format is discarded and you get a notice on save.
- Where it loads: on all public pages. Never in the dashboard, the editor, previews, the version history, feeds or the REST API.
- Cache: on save, BuilderOS clears the page caches so the change reaches all pages.
Connections
In Tracking → Connections, in groups: Google (GTM, GA4 and Ads with its tag on the same card), ad pixels (Meta, TikTok, LinkedIn) and behavior analytics (Clarity). “Configured” only means an ID is saved: the account is not checked.
| Tool | Identifier | Notes |
|---|---|---|
| Google Tag Manager | GTM-XXXXXXX | Recommended: from GTM you add GA4, Ads, Meta and more without touching the site. |
| Google Analytics 4 | G-XXXXXXXXXX | Leave it empty if GA4 is already loaded from Tag Manager (it would count twice). |
| Google Ads | AW-XXXXXXXXX | Google Ads global tag. |
| Google Ads conversion | label (AbC-D_efG-h12) | With it, each form submission counts as a conversion. |
| Meta Pixel | pixel number | Facebook and Instagram. Sends PageView on every page. |
| Microsoft Clarity | project ID | Heatmaps and recordings. |
| TikTok Pixel | pixel ID | Sends page on every page. |
| LinkedIn Insight | Partner ID |
Custom code
Its own screen (?tab=code, item of the Home wheel and of the search; same builder_os_tracking option, same permissions, no migration). It is saved with the same bos_tracking action and the bos_scope=code field, which only changes head, body and footer; Connections keeps the code if it doesn’t send it.
Editor window. Only the presentation changes: a dark window (VS Code Dark+ colors in both dashboard themes) with a title bar (path sitio › codigo › head.html and a Save code button), explorer (SITE CODE: head.html, body.html, footer.html with their size; HELP: Security, Languages, Single-page code, Where it doesn’t load), tabs with a neutral unsaved-changes mark and a status bar (line and column, spaces, UTF-8, HTML, line count, saved). The three textarea fields (head, body, footer) stay in a single form; only one is visible. It uses WordPress’s CodeMirror with wp_enqueue_code_editor( array( 'type' => 'text/html' ) ), only on this screen (highlighting, lines, active line, brackets, Tab indents, no lint); if it returns false (syntax highlighting turned off in the profile) or the JS fails, monospaced text boxes remain with the same tabs. Ctrl/Cmd+S saves and the browser warns on leaving with changes. Code: Builder_OS_Tracking::render_code(), admin/js/builder-os-tracking.js, admin/css/builder-os-tracking.css (.bos-vs).
For everything else: domain verification, chats, pixels from other platforms, a provider’s scripts…
| Place | Purpose |
|---|---|
Header (<head>) | Verification meta tags and scripts that must load first. |
Start of body (<body>) | The <noscript> tags some tools ask for. It needs a theme that calls wp_body_open (all modern ones do, and BuilderOS when it renders the whole site). |
Footer (</body>) | Chats, widgets and scripts that don’t need to load earlier. |
The code is inserted as is. Only administrators with permission for unfiltered HTML (unfiltered_html) can save it; on a multisite, the super admin.
Page code
Besides the global code, each page or post can carry its own. In the editor, open the document settings (right sidebar) and the “Page code” panel: two fields, Header (<head>) and Footer (</body>), with the HTML or JS that loads only on that page (after Tracking’s global code). Only administrators with unfiltered_html see and edit it; an editor without permission doesn’t see the panel and the REST API rejects the change.
- It is saved in the metas
_bos_head_codeand_bos_footer_code(registered withregister_post_meta,show_in_restandauth_callback= administrator withunfiltered_html), so they can also be read and written through the REST API (/wp/v2/pages/<id>,metafield) and with MCP. - It is not a classic meta box: in WordPress before 6.7 a meta box disables the editor’s iframe canvas and breaks the scaled canvas.
- Code:
admin/js/builder-os-page-code.js(PluginDocumentSettingPanelpanel) andBuilder_OS_Tracking::register_page_code_meta(). - For the AI that creates the package: per-page code is not part of the package. The client administrator edits it in the editor, or the AI through REST or MCP with those two metas.
Options
- Don’t measure logged-in users who edit the site (on by default): this way the statistics aren’t polluted by the client’s or the team’s visits.
- Send forms as events (on by default): each successful submission of a BuilderOS form generates:
dataLayer.push({ event: 'bos_form_submit', form_id: '<id>' }), to create the trigger in Tag Manager;generate_leadin GA4 and, if there is a conversion label, the Google Ads conversion;Leadin Meta andSubmitFormin TikTok.
- Google consent mode v2 (off by default): everything is denied (
ad_storage,analytics_storage,ad_user_data,ad_personalization) until a cookie banner callsgtag('consent', 'update', {...}). With the BuilderOS banner (next section) the mode is managed automatically and this switch isn’t needed. It now also lives in Tracking → Cookies → General, under “External cookie manager” (“Google consent mode v2 without the BuilderOS banner”), for those who use another manager, usually from GTM or the header code. Withconsenton and the banner off, everything works as before.
WooCommerce events
With WooCommerce active and the WooCommerce events switch on (Tracking → Connections → Options; woo key of builder_os_tracking, true by default; not shown without WooCommerce), the public site sends the e-commerce events from the browser in the GA4 schema: items[] with item_id (SKU, or the ID if there is none), item_name, price, quantity, item_category, plus currency and value. Prices follow the store’s tax setting and the currency is the store’s. Class Builder_OS_Woo_Events.
| GA4 / dataLayer event | When | Meta | TikTok |
|---|---|---|---|
view_item | Product page | ViewContent | ViewContent |
view_item_list | Shop, category or tag (max. 20 products) | (no) | (no) |
add_to_cart | Classic AJAX, non-AJAX form and blocks (Store API) | AddToCart | AddToCart |
remove_from_cart | Remove or lower quantity (classic and blocks) | (no) | (no) |
view_cart | Cart page | (no) | (no) |
begin_checkout | Checkout page | InitiateCheckout | InitiateCheckout |
add_payment_info | When choosing a payment method | AddPaymentInfo | AddPaymentInfo |
purchase | Thank-you page | Purchase | CompletePayment |
- Google Ads. On purchase the conversion is sent (
send_to,value,currency,transaction_id) if there is a conversion ID and the Purchase label (keygads_purchase, same format asgads_label, which remains the one for forms). - dataLayer. Each push is preceded by
ecommerce: null.gtagis also used only if GA4 is loaded from BuilderOS and not from GTM. - Consent. With the banner’s
window.bosCk: Analytics governs GA4 and dataLayer; Marketing, Meta, TikTok and Ads. Without a banner everything is sent. - Deduplication. The browser purchase’s
event_idisbos-order-<ID del pedido>(dataLayerevent_id, MetaeventID, TikTokevent_id), the same as the server conversion’s (Builder_OS_Conversions::order_event_id). Thetransaction_idis the order number for GA4 and Ads, and the server send to GA4 uses it too. The purchase is printed only once per order (meta_bos_woo_purchase_sent), only with the valid order key in the URL: reloading the thank-you page doesn’t repeat it. - Hook. The
builder_os_woo_events_configfilter receivesarray $cfgto change or add events before they are printed.
In this chapter
For the AI that creates the package
- Don’t put tracking codes (GTM, GA4, pixels) in the package’s HTML or JS: they are configured in BuilderOS → Tracking so the client can change them without touching the design.
- If the site needs to measure specific clicks (a WhatsApp button, a download), add a stable class or
idto the element so the Tag Manager trigger can find it; don’t usedata-bos-*attributes, which are removed on the published site. - Cookies: if the site uses analytics or ads, recommend turning on Tracking → Cookies; don’t install another cookie plugin. Every third-party script added in Custom code must be assigned its category (Analytics, Marketing, Necessary or Preferences); if none is chosen, it is Marketing. Verification meta tags never wait. For a “Configure cookies” link in the footer:
[bos_cookies]orhref="#bos-consent". - A/B tests: they respect analytics consent (the BuilderOS banner or this screen’s consent mode). With the mode on, anyone who doesn’t give analytics consent sees the original and isn’t measured (chapter 28).
For the developer
- Settings in the
builder_os_trackingoption. - Filter
builder_os_tracking_active(boolean) to disable output on certain pages. - Forms fire the browser event
bos:form-senton the<form>; you can listen to it for other tools.