Tracking Scripts

Manage and inject tracking scripts (Google Analytics, Google Ads, Facebook Pixel, custom) into site pages.

TrackingScripts Module for ProcessWire

Manage and inject tracking scripts (Google Analytics, Google Ads, Facebook Pixel, custom code) into site pages, with optional PrivacyWire consent integration and robots.txt/llms.txt file management.


Features


  • Google Analytics (GA4) — inject gtag.js with Measurement ID
  • Google Ads — inject gtag.js with Ads conversion ID
  • Facebook Pixel — inject Pixel tracking code with noscript fallback
  • Custom code — free-form textareas for any third-party scripts (head and/or body)
  • PrivacyWire integration — when enabled, scripts are injected with data-category attributes and type="text/plain" so they only load after user consent
  • robots.txt & llms.txt — edit and auto-generate both files from the admin; content is written to the site root on save
  • Per-service controls — enable/disable, position (head or body), and consent category for each service independently
  • ID validation — regex validation for GA (G-), Ads (AW-), and Pixel (numeric) IDs before injection
  • Admin-only exclusion — scripts are never injected on admin or form-builder templates

Files


site/modules/TrackingScripts/
├── TrackingScripts.info.php                    ← module metadata
├── TrackingScripts.module.php                  ← main module (hooks, script injection)
├── TrackingScriptsConfig.php                   ← module configuration (ModuleConfig)
├── ProcessTrackingScriptsConfig.info.php        ← Process module metadata
└── ProcessTrackingScriptsConfig.module          ← admin UI for non-superusers

Installation


  1. Copy the TrackingScripts folder into /site/modules/

  2. In the admin go to Modules → Refresh, then install TrackingScripts

  3. Optionally install ProcessTrackingScriptsConfig — this adds a Setup → Tracking Scripts page that allows non-superuser roles to edit the configuration. Assign the tracking-scripts-config permission to any role that needs access.


Configuration


Go to Modules → Configure → TrackingScripts (superuser) or Setup → Tracking Scripts (any user with permission).

Google Analytics

FieldDescription
EnableActivate/deactivate injection
Measurement IDGA4 ID, e.g. G-XXXXXXXXXX
PositionInject in <head> or before &lt;/body&gt;
PrivacyWire CategoryConsent category (default: Statistics)
FieldDescription
EnableActivate/deactivate injection
Ads IDe.g. AW-XXXXXXXXX
PositionInject in <head> or before &lt;/body&gt;
PrivacyWire CategoryConsent category (default: Marketing)

Facebook Pixel

FieldDescription
EnableActivate/deactivate injection
Pixel IDNumeric ID, e.g. 123456789012345
PositionInject in <head> or before &lt;/body&gt;
PrivacyWire CategoryConsent category (default: Marketing)

Custom Tracking Code

Two free-form textareas for any additional third-party code:

  • Custom Code — Head: injected before </head>
  • Custom Code — Body: injected before &lt;/body&gt;

PrivacyWire Integration

When enabled, all tracking scripts are rendered with PrivacyWire-compatible attributes:

<script type="text/plain" data-type="text/javascript" data-category="statistics" class="require-consent" src="..."></script>

This ensures scripts only execute after the user gives consent for the corresponding cookie category. Requires the PrivacyWire module to be installed and active.

Robots.txt & LLMs.txt

Edit the content of both files directly from the admin. On save, the files are written to (or removed from) the site root:

  • /robots.txt — search engine crawler directives
  • /llms.txt — LLM/AI bot directives

If a textarea is left empty, the corresponding file is deleted from the site root.


How It Works


The module hooks into Page::render (priority 100) to inject scripts via str_replace on </head> and &lt;/body&gt;. This means:

  • No template modifications required
  • Works on all front-end pages automatically
  • Runs before PrivacyWire (priority 101), so consent attributes are in place when PrivacyWire processes the page

The robots.txt and llms.txt files are written via a hook on Modules::saveConfig, triggered whenever the module configuration is saved from either the module config screen or the Process admin page.


ProcessTrackingScriptsConfig (Admin UI)


A Process module that mirrors the full TrackingScripts configuration under Setup → Tracking Scripts.

Purpose

Allows non-superuser roles to manage tracking scripts without access to the Modules admin.

Permission

The module registers the permission tracking-scripts-config. To grant access:

  1. Go to Access → Roles
  2. Edit the desired role
  3. Check tracking-scripts-config
  4. Save

How it works

  • Reads and writes the same configuration data as TrackingScripts via $modules->getConfig() / $modules->saveConfig()
  • Changes from either location (Modules → Configure or Setup → Tracking Scripts) are reflected in both
  • Saving triggers the same Modules::saveConfig hook, so robots.txt/llms.txt files are written automatically

Requirements


  • ProcessWire 3.0.110+
  • PHP 7.2+
  • PrivacyWire (optional, for consent integration)

License


Licensed under the MIT License.

More modules by lexsanchez

  • Video or Social Post Embed

    Enter a full YouTube, Vimeo, Instagram, Facebook, Twitter or Issuu URL by itself in any paragraph
  • Tracking Scripts

    Manage and inject tracking scripts (Google Analytics, Google Ads, Facebook Pixel, custom) into site pages.
  • Font Awesome Icons Textformatter

    Converts shortcuts like :fa-star: to Font Awesome icons

All modules by lexsanchez

Install and use modules at your own risk. Always have a site and database backup before installing new modules.