AMP自动广告(amp-auto-ads)代码放置咨询及显示异常求助
Hey there, let's get to the bottom of your amp-auto-ads issue. That "Unexpected end of JSON input" error typically means your ad configuration isn't being fetched or parsed correctly, but first let's make sure you've placed all the required code exactly where it needs to be, per the amp-auto-ads documentation:
1. Add the amp-auto-ads component script to your <head> section
You need to include the component's script in the <head> of your AMP page. Place this line before any stylesheets or other custom scripts, and alongside your other AMP component scripts:
<script async custom-element="amp-auto-ads" src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js"></script>
The async attribute is critical here—don't remove it, as it ensures the script loads without blocking the rest of your page.
2. Place the <amp-auto-ads> tag immediately after the opening <body> tag
This is the most important placement rule: the <amp-auto-ads> element must be the first direct child of your <body> tag. It can't be nested inside another container, and it can't come after other content. Here's the base code for AdSense (adjust based on your ad network):
<body> <amp-auto-ads type="adsense" data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"> </amp-auto-ads> <!-- Rest of your page content goes here --> </body>
If you're using a custom configuration file (which is likely where your JSON error is coming from), your tag will look like this:
<amp-auto-ads type="adsense" data-ad-client="ca-pub-XXXXXXXXXXXXXXXX" data-config-url="https://your-domain.com/path/to/valid-config.json"> </amp-auto-ads>
Quick note on that JSON error:
Double-check the URL in data-config-url—make sure it's publicly accessible, and that the file returns valid, complete JSON. A missing closing bracket, trailing comma, or empty response will trigger that "Unexpected end of JSON input" error. Use a JSON validator tool to confirm your config file is error-free.
3. Verify additional network-specific attributes
Depending on your ad provider (like DoubleClick or Ad Manager), you may need to add extra attributes. For example, DoubleClick might require data-ad-unit instead of data-ad-client. Make sure these attributes are included directly in the <amp-auto-ads> tag, not nested inside other elements.
To recap:
- Script in
<head>(async, correct src) <amp-auto-ads>as first child of<body>- Valid JSON configuration (if using a custom config URL)
That should fix both the placement issue and the JSON parsing error you're seeing.
内容的提问来源于stack exchange,提问作者DesignAdvertising

