How to add datalayer.push on wordpress functions.php?

by darrion.kuhn , in category: PHP General , 8 months ago

How to add datalayer.push on wordpress functions.php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 8 months ago

@darrion.kuhn 

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

  1. Access your WordPress website files using an FTP client or through cPanel's File Manager.
  2. Locate the theme folder you are currently using for your website. Usually, it is found under wp-content/themes.
  3. Open the theme folder and find the functions.php file. Make a backup of this file before making any changes.
  4. Edit the functions.php file using a code editor.
  5. Add the following code at the end of the file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
add_action('wp_head', 'add_datalayer_push');

function add_datalayer_push() {
    ?>
      <script>
            dataLayer.push({
                'key': 'value'
            });
      </script>
    <?php
}


Replace 'key': 'value' with your specific data that you want to push into the data layer. You can add multiple key-value pairs or complex objects if needed.

  1. Save the file and upload it back to the server, replacing the existing functions.php file.
  2. Clear any caching plugins or CDN caches to ensure the changes are visible.


Now, when a visitor loads a page on your WordPress website, the add_datalayer_push function will be triggered, adding your specified data to the data layer.