Pragmatic Web Media

How to target ads on specific URL? – Ready made Solution.

Plenty of small publishers does not have web developer. They want to target or show ads to the specific page or URL but it is difficult if they don’t have web developer or coding skills.

If you see this article you need not to be worry here after. In this article you can see the ready made solution for targeting ads using URL.

Do I need Coding Skills?

Absolutely NO!!

How to do that?

This need to be implement using Key value targeting method. There are two methods in key value targeting.

  1. Slot level targeting
  2. Page level targeting

We are going to use page level targeting because URL key value should applicable to all ad units available on the page. So that ads can be targeted within that page.

Steps to implement URL targeting

Step 1: Define variable to fetch ‘pathname’ in the website URL.

Example: https://pragmaticwebmedia.com/GAM-URL-Targeting/

In the above URL domain name is “https://pragmaticwebmedia.com” and pathname is “/GAM-URL-Targeting/”

Step 2: Domain name is common across the site, better to use pathname for targeting. To get path name via JavaScript, use the below code

var pathname = window.parent.location.pathname;

Step 3: GAM has the restriction in targeting which allows 40 characters for targeting. So pathname needs to be truncated. To truncate define length variable.

var length = 40;

Step 4: Use substring function to truncate the pathname by 40characters

var url = pathname.substring(0,length);

Where to add those codes?

Step 5: Add all the variables mentioned on the previous step on the GPT tag inside “googletag.cmd.push(function() {

googletag.cmd.push(function() {
    
    var pathname = window.parent.location.pathname;
    var length = 40;
    var url = pathname.substring(0,length);

How to pass the URL in Key Value Targeting?

Step 6: Add the “ googletag.pubads().setTargeting(‘url’, url); ” code just above the “googletag.enableServices();”

googletag.pubads().setTargeting('url', url)
   .setTargeting('Test', ['refresh']);
    googletag.enableServices();

Code Sample – Full GPT Tag

<script>
  googletag.cmd.push(function() {
    
    var pathname = window.parent.location.pathname;
    var length = 40;
    var url = pathname.substring(0,length);
    
    //Adslot 1 declaration
    gptadslots.push(googletag.defineSlot('/6355419/travel', [[728,90]], 'div-gpt-ad-3720665-1')
                             .addService(googletag.pubads()));

   
googletag.pubads().setTargeting('url', url)
   .setTargeting('Test', ['refresh']);
    googletag.enableServices();
  });
</script>

How to check URL is passing on the GPT Tag?

Step 7: Once the tag is implemented with above changes. Go to the Test page link

Step 8: Enable Google Console to check Key values are passing correctly or not. To enable Google console append “?googfc” on the URL.

Step 9: Once the Google Console is enabled. You will see the overlay on the creative and ad slot details on the bottom of the page. See example below

Step 10: Click “Open targeting info”. Under page level targeting , URL key value will appear.

How to target this in GAM line item?

Step 11: Create URL key value in the GAM UI.

Inventory —> Key-values —> New Key-value —> Name —> url —> Value type —> Dynamic —> Report on values –> Include values in reporting —> Save

Step 12: Goto the line item where you want to target specific URL. Open Customized targeting section and select URL is “targeting URL”. From the page level targeting value in step 10.

Step 13: Save the line item and upload creatives and set live. URL targeting is done now.

Hope this article helps you. Please support and provide feedback.

Feel free to contact us if you need any help in tagging.

*The information contained in this post is for general information purposes only. The information is provided by How to target ads on specific URL? - Ready made Solution. 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.

Exit mobile version