Forum Moderators: martinibuster
Those of us who have ecommerce sites that are using templates that come with the ecommerce host we cannot adjust everything like you can when you build your own website.
I don't have the time to add AdSense code to every single page I want it on manually, so I have it added to the template, but that makes it appear on every page and there are some pages I want the ads to NOT show up if that makes sense.
Thanks in advance for any help.
For example: The eCommerce pages might have an area where the shopping cart resides. In the same area of your non-eCommerce pages, maybe the shopping cart is replaced with something else... maybe you can add your AdSense code to the module that displays the "something else."
<?
$currurl = curPageURL(); // assigns full URL of current page to variable
$currurl = ltrim($currurl, 'https://');
$currurl = ltrim($currurl, 'http://'); // removes URL prefix
if ($currurl != "www.example.com/red-widgets.php" AND $currurl != "www.example.com/white-widgets.php" AND $currurl != "www.example.com/blue-widgets.php")
{
?>
AdSense Code Goes Here
<?
}
?>
A better way is to place the code in a separate file and use REQUIRE as follows ...
1. Add following code to a new file on its own (no headers etc) and save in the root as adsense-urls.php
<?
$currurl = curPageURL();
$currurl = ltrim($currurl, 'https://');
$currurl = ltrim($currurl, 'http://');
if ($currurl != "www.example.com/red-widgets.php" AND $currurl != "www.example.com/white-widgets.php" AND $currurl != "www.example.com/blue-widgets.php")
{ $showas = 1; } // variable that controls whether AS is delivered.
?>
2. In the template replace the AS code with ...
<?
require ($_SERVER['DOCUMENT_ROOT']."/adsense-urls.php");
if ($showas == 1) {
?>
AdSense Code Goes Here
<?
}
?>
With this method, any changes to the excluded URLs can be made in the adsense-urls.php file and have an immediate global affect.
If you are looking to exclude AS from all pages within certain locations, eg www.example.com/help/ and www.example.com/contact/, then you can replace the argument above with ...
if (strpos($currurl, "example.com/help") === FALSE AND strpos($currurl, "example.com/contact") === FALSE)
{ $showas = 1; }
As with all PHP, and especially when using REQUIRE, test off-line first. An error in a REQUIRE file will prevent all pages that call it from loading.
This is a primitive way of controlling the deployment of AdSense, but perfectly effective if you only have a handful of URLs (or paths) that you wish to exclude. For anything more elaborate you should look to storing the criteria in a database. Our AS deployment system doesn't work on URLs, but takes several other factors into account such as page type, visitor location and volume of content before deciding what (if any) ads to display.
Hope this helps.
Dick
Yep, my PHP solution is dependent upon on what your server can run, but I figured I'd be optimistic and post it. Without knowing more about the limitations we have to work with it isn't possible to give you a definitive answer, except maybe javascript, but that wouldn't be my choice in this situation.
A quick fix could be to break the pages away from the template and create straight html files from the final source code. However, that depends on whether the pages run any server side code (best sellers, top 10s etc), and also assumes you have FTP access.
Dick
I realised after my last post that breaking templates won't work for you, and that you probably can't use any server side code on your templates. If that's the case, here's the javascript solution ...
<div id="adsense" style="display: none">
Adsense Code Goes Here
</div>
<script type="text/javascript">
if(document.location.href != "http://www.example.com/red-widgets.htm" && document.location.href != "http://www.example.com/white-widgets.htm" && document.location.href != "http://www.example.com/blue-widgets.htm")
{
document.getElementById('adsense').style.display = '';
}
</script>
Not to claim any undue credit, I've modified code from a previous post by Drag_Racer, [webmasterworld.com...]
As ever, hope this helps.
Dick
[edited by: Play_Bach at 12:17 pm (utc) on June 18, 2009]
create static HTML files
That'd be the way I'd go if possible (although server side code for shopping cart's a likely issue), but not sure how flexible these ecommerce packages are - no experience of them here.
I should also point out that the javascript code I posted hides the AdSense code, it doesn't stop the page requesting it. The only way I know how to do that is to insert code between the javascript start/end tags of the G code, and that could be seen as modifying the code, thus in violation of the TOS. Everyone I know who has done this has sought (and received) permission from G. That may not be so easy these days. Maybe someone here knows more or has another way ...
Dick
[edited by: ThirdWheel at 12:28 pm (utc) on June 18, 2009]