The purpose of the gist in this and other posts is to show examples of how things would work if you would like to use a given functionality in an AMP-compatible way, but don’t have a plugin providing that functionality in AMP yet. Make sure to check if there is a plugin that supports AMP for your functional or visual needs. Note also that these gists are not supported plugins, and that you should take advantage of them in the same way that you use code as you learn new technologies.
This post captures a knowledge aspect gathered from the Official AMP plugin Support forum. The community asks interesting and important questions in the forum, and the engineering team driving the development of the plugin provides equally cool answers worth spreading.
Can you please let me know what is the proper way to add auto amp AdSense code to your plugin? I am using the
@manux_osOfficial AMP plugin user
Website monetization is the process of converting existing traffic being sent to a particular website into revenue. The most common way of monetizing websites is via advertising. And AdSense is one of the most popular advertising platforms nowadays, which allows you to monetize your site by displaying ads.
A powerful new way to place ads on your site is to use AdSense Auto ads. Auto ads use machine learning to make smart placement and monetization decisions on your behalf, saving you time. Place one piece of code just once to all of your pages, and let Google take care of the rest. Some of the benefits of Auto ads include:
- Optimization: Using machine learning, Auto ads show ads only when they are likely to perform well and provide a good user experience.
- Revenue opportunities: Auto ads will identify any available ad space and place new ads there, potentially increasing your revenue.
- Easy to use: With Auto ads you only need to place the ad code on your pages once. When you’re ready to use new features and ad formats, simply turn them on and off with the flick of a switch — there’s no need to change the code again.
The Right Way
Similar to the scenario for injecting GTM functionality, the proper way to enable AdSense in your AMP WordPress site is to use a plugin which provides the capability in an AMP compatible way.
There are surely many available options for Ad Sense plugins for WordPress. Not many of them ensure AMP compatibility, but some do. A couple of examples are the Advanced Ads plugin, and the Ads for WP plugin. If you want to work with an Ads plugin which does not support AMP, we encourage you to reach out to the authors of that plugin and request the feature of AMP compatibility, and tell them to reach out to us if they have questions or need help.
If you can’t or don’t want to use these plugins at all, you still can follow the developer route and the plugin will provide the support you need to make sure that any markup you inject into your AMP pages is valid AMP, and that only valid AMP will be served from your site.
As a specific example, the following plugin contains code to inject AMP Auto Ads to your WordPress AMP pages:
You can use this
gist code directly on your site, and you just have to replace the line defining the
AD_CLIENT with the data associated with your
Ad client id. See the installation instructions at the end of the gist for guidance. Thank you Weston Ruter!