Skip to main content
wpcrux.com

Back to all posts

How to Add Datalayer.push on Wordpress Functions.php?

Published on
7 min read
How to Add Datalayer.push on Wordpress Functions.php? image

Best Tracking Enhancements for WordPress to Buy in October 2025

1 MOTAS Scoring Booklet, Treatment and Assessment Scale for Autism, Progress Tracking Tool

MOTAS Scoring Booklet, Treatment and Assessment Scale for Autism, Progress Tracking Tool

BUY & SAVE
$29.95
MOTAS Scoring Booklet, Treatment and Assessment Scale for Autism, Progress Tracking Tool
2 The Ultimate Network Marketing Tracking Tool: Business Organizer and Monthly Tracker for Leads, Followups, Daily Activity Tracking, Goal Setting - MLM ... CRM on paper - Business Planner (Non-Dated)

The Ultimate Network Marketing Tracking Tool: Business Organizer and Monthly Tracker for Leads, Followups, Daily Activity Tracking, Goal Setting - MLM ... CRM on paper - Business Planner (Non-Dated)

BUY & SAVE
$12.00
The Ultimate Network Marketing Tracking Tool: Business Organizer and Monthly Tracker for Leads, Followups, Daily Activity Tracking, Goal Setting - MLM ... CRM on paper - Business Planner (Non-Dated)
3 2pcs Spike Wrench Track Tool for Shoes, Steel Sports Shoe Replacement Nail Puller and Field Outdoor Running Studs Cleats Removing Aid Tool (Green)

2pcs Spike Wrench Track Tool for Shoes, Steel Sports Shoe Replacement Nail Puller and Field Outdoor Running Studs Cleats Removing Aid Tool (Green)

  • LIGHTWEIGHT & COMPACT DESIGN FOR EASY HANDLING AND PORTABILITY.
  • ESSENTIAL TOOL FOR QUICK SPIKE INSTALLATION AND REMOVAL IN SPORTS.
  • ANTI-RUST, ANTI-CORROSION METAL ENSURES DURABILITY AND LONG-LASTING USE.
BUY & SAVE
$5.99
2pcs Spike Wrench Track Tool for Shoes, Steel Sports Shoe Replacement Nail Puller and Field Outdoor Running Studs Cleats Removing Aid Tool (Green)
4 HO SCALE ADJUSTABLE PARALLEL TRACK TOOL (2/pk) - HO Scale

HO SCALE ADJUSTABLE PARALLEL TRACK TOOL (2/pk) - HO Scale

  • ENSURES PERFECT SPACING FOR SEAMLESS TRACK ALIGNMENT.
  • VERSATILE USE ON STRAIGHT AND CURVED TRACKS FOR FLEXIBILITY.
  • DUAL MEASUREMENT RULERS FOR PRECISE ADJUSTMENTS IN ANY SCALE.
BUY & SAVE
$32.04 $34.34
Save 7%
HO SCALE ADJUSTABLE PARALLEL TRACK TOOL (2/pk) - HO Scale
5 LAJIAOZ Garage Door Vertical Track Repair Tool Set, Anvil Tools for Garage Door Track Maintenance, Exclusively for 2-inch Roller Track Rail Systems Replacment

LAJIAOZ Garage Door Vertical Track Repair Tool Set, Anvil Tools for Garage Door Track Maintenance, Exclusively for 2-inch Roller Track Rail Systems Replacment

  • PERFECT FIT FOR 2-INCH ROLLER TRACK SYSTEMS, GUARANTEED RELIABILITY.
  • PROFESSIONAL-GRADE TOOLS FOR EFFICIENT GARAGE DOOR MAINTENANCE TASKS.
  • COMPLETE SET FOR VERSATILE RESIDENTIAL AND COMMERCIAL TRACK REPAIRS.
BUY & SAVE
$109.99 $129.99
Save 15%
LAJIAOZ Garage Door Vertical Track Repair Tool Set, Anvil Tools for Garage Door Track Maintenance, Exclusively for 2-inch Roller Track Rail Systems Replacment
6 GARDENIX DECOR Self Watering Pots for Indoor Plants 3 Pack - Flower Pot with Water Level Indicator for Plants, Grow Tracking Tool - Self Watering Planter Plant Pot - Coco Coir - Terracota

GARDENIX DECOR Self Watering Pots for Indoor Plants 3 Pack - Flower Pot with Water Level Indicator for Plants, Grow Tracking Tool - Self Watering Planter Plant Pot - Coco Coir - Terracota

  • DURABLE, UNBREAKABLE DESIGN PREVENTS LEAKS & PROTECTS YOUR DECOR.
  • SMART SELF-WATERING FEATURE ENSURES PLANTS THRIVE FOR 10+ DAYS!
  • INCLUDES ORGANIC COCO COIR FOR OPTIMAL WATER RETENTION & GROWTH.
BUY & SAVE
$29.88
GARDENIX DECOR Self Watering Pots for Indoor Plants 3 Pack - Flower Pot with Water Level Indicator for Plants, Grow Tracking Tool - Self Watering Planter Plant Pot - Coco Coir - Terracota
7 Bachmann Industries Adjustable Parallel Track Tool (2 Pack), Small

Bachmann Industries Adjustable Parallel Track Tool (2 Pack), Small

  • MAINTAIN PERFECT SPACING FOR SMOOTH TRACK LAYOUTS.
  • COMPATIBLE WITH STRAIGHT AND CURVED N SCALE TRACKS.
  • ENSURES CONSISTENT CENTER DISTANCE FOR RELIABLE PERFORMANCE.
BUY & SAVE
$27.99
Bachmann Industries Adjustable Parallel Track Tool (2 Pack), Small
+
ONE MORE?

To add datalayer.push on WordPress functions.php, you need to follow these steps:

  1. Access your WordPress dashboard.
  2. Go to "Appearance" and click on "Theme Editor."
  3. In the right-hand side navigation, click on "Theme Functions" (functions.php) under the "Theme Files" section.
  4. Locate the opening PHP tag '
  5. Add the following code snippet below the opening PHP tag:

add_action('wp_head', 'custom_data_layer');

function custom_data_layer() { ?>

<script>
    (function(window, dataLayer) {
        // Add your custom data to the dataLayer here
        // Example: dataLayer.push({'event': 'customEvent'});
    })(window, dataLayer || \[\]);
</script>
<?php

} add_action('wp_footer', 'add_datalayer_push');

  1. After adding the code, click on "Update File" to save the changes.

With this code added to functions.php, the datalayer.push function will be automatically added to the footer of your WordPress site's pages. You can modify the code inside the add_datalayer_push function to include your desired data in the dataLayer.push function call.

How to pass variables using datalayer.push on functions.php in WordPress?

To pass variables using the datalayer.push function in your WordPress functions.php file, you can follow these steps:

  1. Open your functions.php file located in your theme's directory.
  2. Inside the functions.php file, create a custom function and enqueue a JavaScript file that will contain the datalayer.push code. This can be done using the wp_enqueue_script function. function enqueue_custom_script() { wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/custom.js', array('jquery'), '1.0', true); } add_action('wp_enqueue_scripts', 'enqueue_custom_script');
  3. Create a new custom.js file inside the js folder of your theme.
  4. Inside the custom.js file, write the JavaScript code for pushing the variable to the data layer. Make sure to use the WordPress wp_localize_script function to pass the PHP variable to the JavaScript file. (function($) { // Custom JavaScript code var myVariable = ''; dataLayer.push({'variableName': myVariable}); })(jQuery); In the above code, replace $your_variable with the actual PHP variable that you want to pass to the data layer.
  5. Save both the functions.php and custom.js files.
  6. Finally, clear any caching plugins or server caches to ensure the changes are reflected on the front-end.

With these steps, you should be able to pass variables to your data layer using datalayer.push in the functions.php file of your WordPress theme.

How to integrate datalayer.push with third-party plugins on functions.php in WordPress?

To integrate datalayer.push with third-party plugins on functions.php in WordPress, you can follow these steps:

  1. Determine which third-party plugin you want to integrate with datalayer.push. Make sure the plugin supports custom JavaScript or has a hook or filter that can be used to inject your code.
  2. Open your WordPress theme's functions.php file. You can find this file by navigating to your WordPress theme directory and locating the file.
  3. In the functions.php file, locate the function or hook that is related to the plugin you want to integrate with datalayer.push. For example, if you want to integrate with a plugin that tracks form submissions, look for hooks related to form submission.
  4. Once you have found the relevant function or hook, add your datalayer.push code. The code might look something like this: function my_custom_function() { ?>
  5. Save the functions.php file.
  6. Test your integration. This may involve submitting a form, loading a specific page, or triggering the event that the third-party plugin is designed to track. Check the browser console or any available analytics tools to verify that the datalayer.push code is firing correctly.

Note: Be cautious while making changes to the functions.php file as it affects the functionality of your WordPress theme. Always take a backup of the file before making any modifications.