Showing posts with label html. Show all posts
Showing posts with label html. Show all posts

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, ASP.net, 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 http://www.mywebsite.com/javascript/adsense.js.

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.

Tags:

Thursday, 8 February 2007

HTML for AdSense Publishers: Aligning Left, Right and Center

This is the first in a series of posts intended to provide AdSense publishers (and, indeed, anyone else using other similar services) with some basic HTML knowledge that will help them display their ad units correctly.

This first post looks at how to align your ads to the left and right and how to center the ads.

Left Aligning

Left aligning is the default way that ads will be displayed, however, if your ad unit is contained inside a page element that is centering or right-aligning its content, you will need to manually left-align. To do so, you can implement either of the following methods:

1) The old-fashioned and now "deprecated" method is as follows

<div align="left">
... your code goes here ...
</div>
2) The better way to do it, using styles is
<div style="text-align:left;">
... your code goes here ...
</div>
(Alternatively, if you're familiar with CSS and external style sheets, you can simply assign a class to the div tag that has a style of text-align:left - but that's another lesson for another day!)

Right Aligning

Right aligning is basically the same as left-aligning, only you simply replace left with right!

1) Thus, the old-fashioned method is:
<div align="right">
... your code goes here ...
</div>
2) Or the better way to do it, using styles is
<div style="text-align:right;">
... your code goes here ...
</div>
Centering

Again, centering is basically the same as left-aligning, only you simply replace left with center!

1) Old-fashioned method is:
<div align="center">
... your code goes here ...
</div>
2) Or the better way to do it is:
<div style="text-align:center;">
... your code goes here ...
</div>
Note: These techniques only align the ads within another page element. If you want the ads to be on the left or right but have other content wrap around them, you need to "float" them ... which will be the subject of the next post in this series.