Skip to main content
Add DATALYR to your WordPress site to track visitors, conversions, and revenue from ads.

Use Cases

E-commerce (WooCommerce) Track product views, cart additions, and purchases. Attribute sales to Meta, Google, or TikTok ads. Blogs & Content Sites Track pageviews, engagement, and newsletter signups. See which content drives traffic from ads. Lead Generation Capture contact form submissions and demo requests. Track cost per lead by campaign. Membership Sites Track signups, subscription starts, and member engagement.

Before You Start

  • You have a DATALYR account
  • You’ve created a workspace
  • You have admin access to your WordPress site

Installation Method

Choose one of these methods:

Method 1: Using a Header/Footer Plugin (Easiest)

Recommended Plugin: Insert Headers and Footers (free)
  1. In your WordPress admin, go to Plugins → Add New
  2. Search for “Insert Headers and Footers”
  3. Click Install Now, then Activate
  4. Go to Settings → Insert Headers and Footers
  5. Paste your DATALYR tracking script in the Scripts in Header box:
<script
  defer
  src="https://track.datalyr.com/dl.js"
  data-workspace-id="YOUR_WORKSPACE_ID">
</script>
  1. Click Save

Method 2: Edit Theme Files Directly

Only use this method if you’re comfortable editing theme files. Changes may be lost on theme updates.
  1. Go to Appearance → Theme File Editor
  2. Click header.php in the right sidebar
  3. Find the </head> closing tag
  4. Paste your DATALYR tracking script right before </head>:
<script
  defer
  src="https://track.datalyr.com/dl.js"
  data-workspace-id="YOUR_WORKSPACE_ID">
</script>
</head>
  1. Click Update File

Method 3: Using functions.php

Add this to your theme’s functions.php file:
function datalyr_tracking_code() {
    ?>
    <script
      defer
      src="https://track.datalyr.com/dl.js"
      data-workspace-id="YOUR_WORKSPACE_ID">
    </script>
    <?php
}
add_action('wp_head', 'datalyr_tracking_code');
Replace YOUR_WORKSPACE_ID with your actual workspace ID from DATALYR.

Get Your Tracking Code

  1. Log in to your DATALYR dashboard
  2. Go to Settings → Tracking
  3. Copy your tracking script
  4. Replace YOUR_WORKSPACE_ID in the examples above

Verify Tracking Works

  1. Visit your WordPress site in a new browser tab
  2. Go back to your DATALYR dashboard
  3. Click Events in the sidebar
  4. You should see a page_view event within 10 seconds
If you see events, you’re done!

What Gets Tracked Automatically

Once installed, DATALYR automatically captures:
  • Page views on all pages and posts
  • Visitor IDs (persistent across sessions)
  • UTM parameters from ad campaigns
  • Ad click IDs (fbclid, gclid, ttclid)
  • Referrer and traffic source
  • Device and browser information

Track Contact Form 7 Submissions

If you use Contact Form 7, add this to track form submissions:
  1. Go to Plugins → Add New
  2. Search for “Code Snippets”
  3. Install and activate the plugin
  4. Go to Snippets → Add New
  5. Add this code:
add_action('wpcf7_mail_sent', function($contact_form) {
    ?>
    <script>
    if (window.datalyr) {
        datalyr.track('form_submission', {
            form_name: '<?php echo $contact_form->title(); ?>',
            form_id: '<?php echo $contact_form->id(); ?>'
        });
    }
    </script>
    <?php
});
  1. Click Save Changes and Activate

Track WooCommerce Purchases

If you use WooCommerce, track purchases automatically: Add this code via Code Snippets or your theme’s functions.php:
add_action('woocommerce_thankyou', function($order_id) {
    if (!$order_id) return;

    $order = wc_get_order($order_id);
    $items = array();

    foreach ($order->get_items() as $item) {
        $product = $item->get_product();
        $items[] = array(
            'id' => $product->get_sku(),
            'name' => $item->get_name(),
            'price' => $item->get_total(),
            'quantity' => $item->get_quantity()
        );
    }
    ?>
    <script>
    if (window.datalyr) {
        datalyr.track('purchase', {
            order_id: '<?php echo $order->get_order_number(); ?>',
            revenue: <?php echo $order->get_total(); ?>,
            currency: '<?php echo $order->get_currency(); ?>',
            items: <?php echo json_encode($items); ?>
        });
    }
    </script>
    <?php
});
This sends purchase events to DATALYR with order details.

Troubleshooting

Not seeing events? Check that:
  • Your tracking script is in the <head> section, not footer
  • You’ve cleared your WordPress cache (if using a caching plugin)
  • Your workspace ID is correct
  • Ad blockers are disabled
Script appearing as text on site? Make sure you pasted the script in:
  • Scripts in Header (not Scripts in Body or Footer)
  • Inside PHP tags if using functions.php
WooCommerce tracking not firing? Clear your site cache and test with a real purchase (use a test mode payment gateway).

Caching Plugins

If you use a caching plugin (WP Rocket, W3 Total Cache, etc.):
  1. Clear your cache after installing DATALYR
  2. Some plugins have a “Never cache” option - add /wp-admin/* if needed
  3. The DATALYR script loads asynchronously and won’t be blocked by most caches

Next Steps

Need Help?

Can’t get tracking working on WordPress? Check our troubleshooting guide or contact support.