In the realm of online publishing, many small-scale publishers encounter a formidable challenge: the ability to precisely target ads to specific articles or pages on their websites. This hurdle often arises from a lack of advanced development skills or budget constraints that hinder them from investing in costly development work. But fear not, for your worries are about to become a thing of the past.
Table of Contents
Introducing Your Solution
We understand the unique needs of small publishers, and we’re here to alleviate your stress. Our dedicated team has crafted a comprehensive solution designed specifically for you. Say goodbye to the struggles of ad targeting, as our website offers a ready-made, user-friendly solution tailored to address this very issue.
No more sleepless nights or complex coding endeavors. With our user-friendly platform, you can effortlessly take control of your ad targeting, making it simpler and more affordable than ever before. Don’t let technical hurdles hold you back any longer. Embrace the future of targeted advertising with our seamless solution, designed to empower small publishers like never before.
Let’s dive into the steps:
Step1: Generating a 16-Character Unique ID from a URL in JavaScript
Step2: Integrate this Unique ID in the GPT Tag.
Generating a 16-Character Unique ID from a URL in JavaScript
When working on web applications, you may encounter scenarios where you need to generate unique identifiers for various purposes. One common requirement is to create a fixed-length 16-character unique ID based on the URL of a web page. This ID should remain constant even when the page is refreshed, making it useful for tracking or identifying specific pages or resources. In this article, we’ll show you how to achieve this using JavaScript.
Prerequisites
Before we dive into the code, you’ll need to include the md5 hashing library in your HTML file. You can do this by adding the following line to your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.18.0/js/md5.min.js"></script>
Generating the Unique ID
Here’s the JavaScript code that generates a fixed-length 16-character unique ID from the URL:
<script>
// Function to generate a fixed-length unique ID from the URL
function generateUniqueIDFromURL() {
const url = window.location.href; // Get the current URL
const hash = md5(url); // Calculate the md5 hash of the URL
const uniqueID = hash.substr(0, 16); // Take the first 16 characters of the hash
return uniqueID;
}
// Get the unique ID and display it
const uniqueID = generateUniqueIDFromURL();
console.log("Unique ID:", uniqueID);
</script>
How It Works
- We start by obtaining the current URL using
window.location.href
. - Next, we calculate the MD5 hash of the URL using the
md5
function. - To create a fixed-length 16-character unique ID, we use the
substr
method to extract the first 16 characters of the hash. - The resulting
uniqueID
is a 16-character string that uniquely represents the URL.
Use Cases
Generating a fixed-length 16-character unique ID from a URL can be helpful in various situations:
- Tracking: You can use this ID to track user interactions with specific pages or resources on your website.
- Caching: It can be used to cache and retrieve content associated with a particular URL efficiently.
- Linking: You might want to create shareable links with a consistent URL identifier.
Integrate this Unique ID in the GPT Tag.
All that’s required is to place the ‘Unique ID’ script at the top of your page, ahead of the GPT script. Afterward, simply pass the ‘uniqueID’ variable into the page-level key value within the GPT Tag.
While these technical terms might seem a bit perplexing at first glance, don’t worry. We’ve provided the complete script below to clear up any uncertainties. Take a look at the comprehensive script provided below to get a better understanding.
Here is the place where page level key value is defined.
googletag.pubads().setTargeting('UniqueID', uniqueID)
Complete Code and Live Page
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.18.0/js/md5.min.js"></script>
<script>
// Function to generate a fixed-length unique ID from the URL
function generateUniqueIDFromURL() {
const url = window.location.href; // Get the current URL
const hash = md5(url); // Calculate the md5 hash of the URL
const uniqueID = hash.substr(0, 16); // Take the first 16 characters of the hash
return uniqueID;
}
// Get the unique ID and display it
const uniqueID = generateUniqueIDFromURL();
console.log("Unique ID:", uniqueID);
</script>
<!-- Start GPT Tag -->
<script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
<script>
var gptadslots = [];
var googletag = googletag || {cmd:[]};
</script>
<script>
googletag.cmd.push(function() {
//Adslot 1 declaration
gptadslots.push(googletag.defineSlot('/6355419/travel', [[728,90]], 'div-gpt-ad-3720665-1')
.addService(googletag.pubads()));
googletag.pubads().setTargeting('UniqueID', uniqueID)
.setTargeting('Test', ['refresh']);
googletag.enableServices();
});
</script>
<!-- End GPT Tag -->
</head>
Click here to see live page example.
How to check the values are passing correctly on the page?
To check the values, enable google console on the page using bookmark or console method. Once the console is enabled, you can see the UniqueID values under the page level targeting. This value need to be used on the line item targeting.
Next Step: Create key value in GAM UI
Inventory —> Key-values —> New Key-value —> Name —> url —> Value type —> Dynamic —> Report on values –> Include values in reporting —> Save
Target Key value in Line Item
Then Goto line item –> Customized targeting –> UniqueID —> Enter the value (Copied from the page using Google console)
Final step save and approve the line item to set live. Then you can serve ads to the specific page easily.
I hope this helps you.
*The information contained in this post is for general information purposes only. The information is provided by How to Target specific Article/Page in Google Ad Manager? and while we endeavor to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the post for any purpose.