Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Adsense in Google Tag Manager

I want to use Google Tag Manager to serve Adsense so I also can use other ads from other sources. I found few answers across the sites but not enough to understand. I would appreciate input here if it is possible to use Adsense code in GTM, if yes how? I also use DFP, if I have to use DFP then how it would be.

like image 771
Shruti Shah Avatar asked Nov 18 '17 12:11

Shruti Shah


1 Answers

I assume you already have Google Tag Manager (GTM) running on your site?

  1. Click on Variables on the left side of the screen within your GTM workspace
  2. Create a new user defined variable and call it asClientID
  3. Configure the variable as a Constant. The value to enter is your AdSense Client ID
  4. Click on Tags on the left side of the screen within your GTM workspace
  5. Create a new Tag called AS - Page view - ALL
  6. Because there is no built-in tag in GTM for AdSense you need to use the Custom HTML tag.
  7. Copy and Paste the HTML code shown below into GTM.
  8. Select the trigger All Page views
  9. Test your changes in preview mode
  10. Once everything is working publish the changes to LIVE.

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "{{asClientID}}",
    enable_page_level_ads: true
  });
</script>

As you can see we are using {{asClientID}} in the script which you have created in step 1 and 2. The reason for creating this variable is flexibility. If you need to change the ID you change it within the variable and not within the code.

NOTE: that it is not best practice to implement AdSense via GTM, but it is possible.

Hopefully this is what you are looking for. Please do let me know if it works for you. :-)

like image 136
Raoul Dundas Avatar answered Oct 31 '22 20:10

Raoul Dundas