Add Dotdigital scripts to your WordPress site

Enable Web behavior tracking or Dotdigital Chat.

Laura Russell avatar
Written by Laura Russell
Updated over a week ago

There are a number of Dotdigital scripts you can add to your site through your Content Management System (CMS), which allow you to collect and transfer data, and trigger certain actions, across both systems.

If you want to gather new sign-ups through your website, you can use the Dotdigital for WordPress plugin to add a signup form or survey, page, or form to your site.

You might also want to enable Web behavior tracking or Chat on your site:

  • Web behavior tracking
    Collect data based on your contacts’ browsing behaviour, allowing you to trigger customised actions; encourage purchase completions, provide related downloads or other content, or send helpful reminders to submit an incomplete form. The script also powers features such as abandoned cart, abandoned browse, and product recommendations.

  • Chat
    Communicate with customers and prospective customers in real-time using a Chat widget embedded directly on your website.

To enable these features, you must add the relevant script from your Dotdigital account to your WordPress site.

We suggest two options you can choose from to do this:


Before you start

  • We recommend you back up your site before performing any updates.


Use a plugin

One of the most popular plugins for adding custom scripts to WordPress is WPCode.

This plugin was previously called Insert Headers and Footers. Availability of the plugin depends on your WordPress plan level.

WPCode allows you to add JavaScript to the header or footer of your site. It also allows you to apply conditions to control which areas of your site the script loads on, or even for which users.

To do this:

  1. Download and install the plugin.

  2. Expand the Code Snippets section of the left side menu, then select + Add Snippet.

  3. Hover over Add your Custom Code (New Snippet) and select Use Snippet.

  4. Enter a name for your snippet, to help you to recognise it in future.

  5. Expand the Code Type drop-down menu and select JavaScript Snippet.

  6. Add your script to the Code Preview field.
    Do not include <script> </script> tags, as the plugin adds these automatically.

  7. Select the Location field to expand the menu, and then select where on your site you want the script to be deployed.
    If you need more granular control than the Location options offer, you can use the Smart Conditional Logic section to create more complex rules for your snippet.

  8. Once happy with your configuration, select Save Snippet.

  9. Toggle the switch at the top of the page to Active.

We’re using WPCode as an example only; there are many other WordPress plugins you can choose from which serve a similar purpose.


Edit the functions.php file

Before you start

  • The following changes require your WordPress developers to implement.

  • If you add code to your theme's functions.php file, make sure to use a WordPress child theme so that your code snippets don't get overwritten when you update the main theme.

  1. In WordPress admin, go to Appearance > Theme Editor.

  2. From the Theme Files menu in the right side panel, select Theme Functions to open the functions.php file.

Add scripts to the header

To add a script to the header of your entire site, use this code snippet:

function dotdigital_javascript() {
?>
// Insert your script here. Make sure to include the opening <script> and closing </script> tags.
<?php
}
add_action('wp_head', 'dotdigital_javascript');

Add scripts to the footer

To add a script to the footer of your entire site, use this code snippet:

function dotdigital_javascript() {
?>
// Insert your script here. Make sure to include the opening <script> and closing </script> tags.
<?php
}
add_action('wp_footer', 'dotdigital_javascript');

Add to a specific post or page

To load the script on a specific post or page only, you can include the ID of the post or page. The code you should use differs slightly for a post and for a page.

Post

For a post, use is_single in an If statement:

function dotdigital_javascript() {
if (is_single ('[pageID]')) {
?>
// Insert your script here. Make sure to include the opening <script> and closing </script> tags.
<?php
}
}
add_action('wp_head', 'dotdigital_javascript');

Page

For a page, use is_page:

function dotdigital_javascript() {
if (is_page ('[pageID]')) {
?>
// Insert your script here. Make sure to include the opening <script> and closing </script> tags.
<?php
}
}
add_action('wp_head', 'dotdigital_javascript');

Did this answer your question?