Thursday, 15 March 2007

Putting AdSense Code in External Files

If you are managing a web site comprised of several pages and you would like to be able to add AdSense code to the site without having to paste the exact same code into each and every page, there are two main ways this can be achieved.

Include Files

Dynamic page building technologies such as ASP,, PHP, and SHTML allow code that is common to multiple pages to be stored in an external file and incorporated into the main page with a single command.

For example, if you were using ASP you could place your AdSense code in a file named i_adsense.asp (the i_ prefix indicating an include file. Note: It is not good practice to name ASP include files with a .inc extension). In order to include this file in your main code, you simply use the command

<!--#include file="i_adsense.asp"-->
Similarly, in PHP you use the command
<?php include("i_adsense.php") ?>

External .JS Files

Using this method, you simply place your AdSense javascript in a file that has a .js extension, such as adsense.js, and reference this file in your main code, wherever you want the AdSense ads to appear. To incorporate the external .js file in your code, you would use the command
<script language="JavaScript" src="inputmasks.js" type="text/javascript"></script>
Note, the src attribute can contain a full path or absolute URL, such as js-files/inputmasks.js or

Is this method permitted?

There seems to be some confusion as to whether external .js files are permitted. In order to clarify this situation, I recently contacted AdSense support and this is how they responded
You're welcome to include the AdSense ad code in .js files, but please don't modify the code or manipulate the ad targeting in any way, as this is against our program policies.
Thus, as long as you don't alter the core JavaScript code in any way, it is perfectly acceptable to make use of external .js files.

Limitations of these Methods

External files make managing your AdSense code across many pages much simpler. This is particularly useful when you are using a template that allows for an ad unit of the same size in the same place on every page in your site. However, this is not without its limitations.

The single biggest issue with this is that it makes the use of precise channels much more difficult. For example, supposing every page has a leaderboard at the top of the page that is in an external include file and you want to include a unique channel for every page's leaderboard. If you had no other AdSense ads on the page, you could make use of URL channels, but what would you do if you also had a skyscraper unit on your pages? How would you differentiate the two?

By incorporating programming techniques into your external include files in ASP, PHP, etc., even this is possible, but is beyond the scope of this post.



Anonymous said...

you cannot put adsense in an external js file as it requires modding the adsense code.

Ian F. said...

Hi. You obviously haven't read my post very closely. AdSense support told me themselves that it's OK.

Garve said...

Sorry to comment on this years later, but I'm surprised this hasn't attracted more discussion.

I'd love to do this to reduce my bandwidth, but other than this post I've been unable to find confirmation that it's acceptable under the TOS. Are you able to say who you contacted and what they said exactly?

Ian F. said...

Garve, below is a copy of the full email I received from AdSense support March 15th, 2007

Hi Ian,

Thanks for your email. You're welcome to include the AdSense ad code in
.js files, but please don't modify the code or manipulate the ad targeting
in any way, as this is against our program policies.

Please note that we monitor all sites displaying Google ads or an AdSense
for search box to ensure compliance with our program policies. Any site
that is in violation of our policies will be contacted and asked to remove
the Google code.

Thanks for your cooperation.

For additional questions, I'd encourage you to visit the AdSense Help
Center (, our complete resource center
for all AdSense topics. Alternatively, feel free to post your question on
the forum just for AdSense publishers: the AdSense Help Group


The Google AdSense Team