How to add Adsense Code in Google Tag Manager
Step by step instructions for implementing Google AdSense Code into a Google Tag Manager (GTM) container and fire it on page view.
How to install Google AdSense [1] using Google Tag Manager (GTM) [2] by creating a variable for the AdSense Client ID and a custom HTML tag for the Adsense code (Google Tag Manager does not support AdSense natively). The final implementation will fire on every page view.
Beware: While it works to ship Google AdSense ads through Google Tag Manager, it is not best practice (not a recommended solution). It is better to implement AdSense directly into your website, as it is more performant and thus user friendly.
Step by step instructions
- Within your GTM workspace, select Variables from the menu.
- In the User-Defined Variables section, create a new user-defined variable with the name AdSenseClientID.
- Set the variable type to Constant. The value to enter is your AdSense Client ID.
- Safe the configuration.
- Within your GTM workspace, select Tags from the menu.
- Create a new Tag called AdSense – Page view – ALL
- In the Tag Configuration, set the tag type to Custom HTML. Unfortunately, there is no built-in tag in Google Tag Manager for AdSense.
- Copy the AdSense code and paste it in the HTML textbox in Google Tag Manager.
- Replace your AdSense client ID with
{{AdSenseClientID}}
, which will insert the previously defined variable. - In the Triggering section for the tag, select the trigger All Pages with the type Page view.
- Safe the configuration.
- If applicable, publish your website to a staging or testing environment where you can test the configuration.
- Withing your GTM workspace, select Preview to test the changes in preview mode.
- Once everything works the way you want it to work, publish to your production environment.
Abbreviations
Abreviations used in this document.
- GTM
- Google Tag Manager