Dynamic Text Replacement Script To Increase Google Search Campaign’s ROI

Dynamic Text Replacement (DTR) is an amazing feature that can increase CTR’s of all your landing pages, so I hope this guide will help get you on the right track.
Personalize your Google Search Ads and Landing Pages Using Dynamic Text Replacement Script And Google Tag Manager.
In this guide, I will show you how to personalize your website’s landing pages by using a simple JS script and Google Tag Manager.
How does Dynamic Text Replacement do?
Dynamic Text Replacement (DTR) feature can automatically substitute your headlines, titles, or other text according to the user’s search query in Google Search Network.
Dynamic Text Replacement Script To Increase Your Google Search Campaigns ROI
1. How to use the Dynamic Text Replacement?
The first thing to start is to determine which text of your website’s page you’d like to replace.
There is such a thing, as – selector – it defines the path (or URL) to the element in the website’s code.
How to find and copy the Selector’s path
1.0. Open your landing page ➜ make a right-click on the headline, and click Inspect.
Click on the icon
Copy ➜ Copy selector
Your selector’s path will look something like this:
#post-2137 > div.td-post-header.td-pb-padding-side > header > h1
Now you’ll need to replace the Selector’s path on your own one in the following script’s variable:
var selector = '#post-2137 > div.td-post-header.td-pb-padding-side > header > h1';
Make sure you paste your path inside the single quotes ‘ ‘
Save this line to a notepad.
2. Campaign Tracking Template
Next, we’ll need to add a tracking template to our Search campaign in Google Ads: {lpurl}?utm_replace={keyword}
2.0. Navigate to your campaign’s Settings ➜ Open Campaign URL options tab ➜ add the tracking template above
Now, when a user will click on our search ad, a specific keyword (search term) from our list will be added as a UTM tag at the end of the URL after redirecting to our website.
3. Content for replacement
Now we need to prepare our content for replacement.
In my case, I’ll be using the following keywords in my Google Search campaign.
- digital marketing trends 2022
- digital marketing trends 2021
- digital marketing trends 2020
- digital marketing trends us
- digital marketing trends canada
- digital marketing trends uk
I want that each of the search queries to be replacing the headline of my article by the following rule:
- digital marketing trends 2022 – Top 15 Digital Marketing Trends For 2022
- digital marketing trends 2021 – Top 15 Digital Marketing Trends For 2021
- digital marketing trends 2020 – Top 15 Digital Marketing Trends For 2020
- digital marketing trends us – Top 15 Digital Marketing Trends In US For 2021-2022
- digital marketing trends canada – Top 15 Digital Marketing Trends In Canada For 2021-2022
- digital marketing trends uk – Top 15 Digital Marketing Trends In UK For 2021-2022
To do so, I’ll need to add variable – content, which will contain my rules as objects.
var content = { digital_marketing_trends_2022: 'Top 15 Digital Marketing Trends For 2022', digital_marketing_trends_2021: 'Top 15 Digital Marketing Trends For 2021', digital_marketing_trends_2020: 'Top 15 Digital Marketing Trends For 2020', digital_marketing_trends_us: 'Top 15 Digital Marketing Trends In US For 2021-2022', digital_marketing_trends_canada: 'Top 15 Digital Marketing Trends In Canada For 2021-2022', digital_marketing_trends_uk: 'Top 15 Digital Marketing Trends In UK For 2021-2022', };
Basically, it’s is like a dictionary that contains desired keywords (digital_marketing_trends_2022, digital_marketing_trends_us, digital_marketing_trends_canada) and values - headers that should be replaced on the website.
There are some requirements so the script would work properly:
- The short name should be without spaces in English, instead of spaces you should use underscores, for example – digital_marketing_trends_2022
- The short name must be preceded by four spaces, as in the example code
- The short name is followed by a colon – :
- The long title is enclosed between single quotes – ”
- There is always a comma after a long title – ,
Now, our complete Dynamic Text Replacement Script will look the following way:
<script> var content = { digital_marketing_trends_2022: 'Top 15 Digital Marketing Trends For 2022', digital_marketing_trends_2021: 'Top 15 Digital Marketing Trends For 2021', digital_marketing_trends_2020: 'Top 15 Digital Marketing Trends For 2020', digital_marketing_trends_us: 'Top 15 Digital Marketing Trends In US For 2021-2022', digital_marketing_trends_canada: 'Top 15 Digital Marketing Trends In Canada For 2021-2022', }; // Replace the Selector URL var selector = '#post-2137 > div.td-post-header.td-pb-padding-side > header > h1'; // don't touch anything below function replacer(content) { var utm2 = /utm_replace=([^&]*)/g.exec(document.URL)[1]; var utm = utm2.replace(/%20/g, "_"); if (utm in content) { document.querySelector(selector).innerHTML=content[utm]; } else { console.log('Best Online Casino'); }; }; replacer(content); </script>
4. Placing the script on the website
There are two ways of placing the script on your website:
- Place the script before the closing </body> tag
- or through the Google Tag Manager
Let’s consider the last method.
Below you’ll find the tutorial on how to install our script with the Google Tag Manager on your website. The sequence is very similar to installing the Google Analytics code.
Navigate to the “Tags” section and create a new tag.
4.0. Navigate to Google Tag Manager ➜ Create New Tag
4.1. Click Edit on Tag Configuration
4.2. Choose tag type ➜ Custom HTML
4.3. Add the script in the field
4.4. In the Triggering select All Pages ➜ And then add another trigger DOM Ready (this will trigger the script once the webpage is loaded)
4.4. Save tag and publish the adjustments
5. How the Dynamic Text Replacement script works
So, you’ve added the keyword list to your Google Search campaign.
Now, when the user clicks on your add he will be redirected to your website.
Because of the tracking template, the redirected URL will contain at the end the exact keyword, which the user entered in the search query.
www.YourSite.com/landingpage?utm_replace={keyword}
In this case, if your ad is triggered for the keyword “Digital Marketing Trends 2021” your final URL will look this:
www.YourSite.com/landingpage?utm_replace=Digital%20Marketing%20Trends%202021
%20 is added because of space. But don’t worry we’ve taken this into consideration so our script could deal with that
Then our script will capture this keyword from the URL and will replace the website’s headline with the text from the keyword value rule we’ve included in the script.
P.S. I hope this article was helpful to you. If you have any questions, suggestions, or you’d like to share something related to the topic, please drop a line in the comment section below.
Cheers 👋