Your ad
Find it in AdSense under Account → Settings → Account information. pub-… is turned into ca-pub-….
Code type
The ad unit's ID. Create the unit in AdSense (Ads → By ad unit) and copy the number shown in its code. It can't be made up.
Added as an HTML comment so you can find the unit in your template later.
Google: shape values other than auto apply on desktop only.
Size must match the fixed size you picked for the unit in AdSense.
AdSense puts it in the In-feed unit's code. Copy it from there.
Code
Before ads show
- Your site has to be added under Sites in AdSense and reach the status Ready.
- Add your ads.txt line:
google.com, pub-…, DIRECT, f08c47fec0942fa0. - For Auto ads, also set up Auto ads for the site in AdSense under Ads. Google generates the snippet only after that.
Auto ads code vs ad unit code
The AdSense code is one script tag,
adsbygoogle.js?client=ca-pub-… with crossorigin="anonymous". Google asks you to put it
between <head> and </head> on every page. With Auto ads switched on, that
is all you need: Google picks where ads go.
An ad unit puts an ad in a spot you choose. Its code is the same script plus an
<ins class="adsbygoogle"> element with your publisher ID (data-ad-client) and the
unit's ID (data-ad-slot), followed by (adsbygoogle = window.adsbygoogle || []).push({});.
The ad slot ID only exists once you create the unit in your AdSense account. A made-up number will not show ads, so this tool formats the code but can't invent the ID for you.
Google allows only certain changes to ad code, such as the shape and full-width options here. Other edits can break the ads or the program policies, so when in doubt copy the code straight from your account.
Frequently asked questions
What is the difference between ca-pub- and pub-?
They are the same account. The ad code uses ca-pub- followed by 16 digits; the ads.txt file uses pub- with the same digits.
Where do I paste the AdSense code?
Google says to paste the AdSense code between the <head> and </head> tags. Ad unit code goes in the HTML where you want that ad to appear.
Where do I get the data-ad-slot number?
From your AdSense account: Ads, By ad unit, then Get code on the unit. Each unit has its own slot ID.
What sizes can a fixed-size ad unit be?
Google's limits: width at least 120px, height at least 50px, neither over 1200px, and only one side may be larger than 450px. Ads outside these limits won't show.
Should I use responsive or fixed-size units?
Google recommends responsive units for responsive sites. Fixed sizes don't adapt to the screen and some sizes have little ad demand.