Forum Moderators: coopster
At the time of this post, the 10th reply was the last one from the adsense thread, to which my reply was:
--------------------
Yeah I will try a few methods this weekend and see which one I can get to work - I think Storm's method might be the better option but I just have one more question:
So for your code, Storm, how would you break it up? Figure you have the individual .php files that are dynamically created, and the main include where the core adsense code goes:
This goes on each individual page:
$news = "324234234";
$tutorials = "4555445";
Does the rest go in the include? Or does the GET statement go in the individual file?
-----------------------------
<original code as posted by storm>
I'm not sure if I understood you. If you're working with PHP you could do something like this:
first list each channels you want to track, that is:
$news = "324234234";
$tutorials = "4555445";
and so on
then make some condition, let's say the name of the category is stored in $_GET['category']
if ($_GET['category'] == "news"){
$google_ad_channel = $news;
}
and so on
finally echo the adsense code and replace the number after "=" with the variable $google_ad_channel. Remember about escaping all quotation marks with "\". The whole code might look similiar to this:
<?php
$news = "324234234";
$tutorials = "4555445";
if ($_GET['category'] == "news"){
$google_ad_channel = $news;
}
echo "<script type=\"text/javascript\"><!--
google_ad_client = \"pub-somepub\";
google_ad_width = 728;
google_ad_height = 15;
google_ad_format = \"728x15_0ads_al_s\";
google_ad_channel =\"$google_ad_channel\";
google_color_border = \"FFFFFF\";
google_color_bg = \"FFFFFF\";
google_color_link = \"993300\";
google_color_text = \"000000\";
google_color_url = \"999999\";
//--></script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>";
?>
In this way you can track many channels.
Hope it helped
Storm
-----------------------------
Any suggestions appreciated. Thanks.
You've got a given number of categories (news, tutorials, etc.). When working inside the individual php file, you can echo the category you are viewing, e.g. echo $_GET['category'];
So each of these categories will be assigned a channel number, like:
$category = $_GET['category'];
if ($category=="news"){
$google_ad_channel = "123214";
}
...
include("google_ad.php"); //
This code can reside either in the individual php file or in the SSI (except for the last line). Now it's time for the contents of SSI, best done in this way:
TIP: clean all tags from the file (<body>, <title>, etc.)
<script type="text/javascript\"><!--
google_ad_client = "pub-somepub";
google_ad_width = 728;
google_ad_height = 15;
google_ad_format = "728x15_0ads_al_s";
google_ad_channel ="<?php echo $google_ad_channel;?>";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "993300";
google_color_text = "000000";
google_color_url = "999999";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
I think it's a better way than the one I suggested earlier
because you don't mess your code with slashes (can be harder to change ad formats from my experience).
I hope I made it a bit clearer for you, if not, don't hesitate to ask.
Storm
The scenario:
To rehash what I was trying to do.. I wanted to integrate Google's Adsense ads into my php pages that were dynamically created with includes. The problem was, I use channels to track all of my ads. Given their placement inside a left or right bar, I wanted to put the ad code inside of the bar, inside of an include, but still have each page maintain their individual channel IDs.
Unless I were to have multiple sets of includes for the left or right bars, I wouldn't have been able to give my ads their channel IDs across a spectrum of pages (reviews, news, articles, etc) And since each of those pages has more than one adsense ad.. it could get messy. The only other option would have been to slice the left/right bar in half with includes and manually enter the adsense code for each page - this would have been a hassle for future upgrades and changing the placement/design of my ads.
I believe the following method allows me to leave my channel IDs for each ad across all of my pages intact, while leaving enough flexibility for future changes to the ad layouts. Heres how I did it:
There are 3 main files to deal with, plus the page that the ads will appear on.
1) ads.php - Here I define every ad across the entire site that uses channel IDs. For example, the "articles" page has 2 ads, one on the left bar, and one on the bottom, both with unique channel IDs so I can track their performance in the adsense control panel. So in ads.php, I define the channel IDs for the home page, articles, news, downloads, etc, and the different ads within. The page will be the central site where I will change/add/remove channels in the future.
2) cat_pagetype.php - This file will reside in each folder of the different page types, (news, articles, etc). So in my articles folder where the main .php file for that page is, there will also be a cat_articles.php file. This file serves as an include to the main output file. I could have hardcoded it's contents in the main file but doing it this way and using it as an include makes it easier for me to make adjustments in the future without having to go back through tons of pages.
3) top.php - This is the main include that serves up the html, head, title, and part of the body code for my site. This area can be tailored many different ways depending on your layout but for this example I am using my site's layout. Incidentally, this file has the code that makes up the left bar of my pages. The core adsense code is in this file (the code generated by google).
4) index.php - Since I put index files in each folder of each category (news, reviews, etc) every file is named the same. This file is what stores the includes for top/bottom.php, ads.php and cat_pagetype.php.
So far hopefully this is all making sense. Now for the code and how it works: The following example is for the "articles" page. But the same method applies to all pages, just with different categories.
Starting with cat_articles.php, this is the code for this file:
<?php
$category = "articles_sky";
$category = "articles_body";
?>
Now for ads.php
<?php
if ($category=="articles_body"){
$google_ad_channel = "123456789";
}
if ($category=="articles_sky"){
$google_ad_channel = "987654321";
}
?>
The numbers are obviously the ones uniquely generated by google, and I would have an IF statement for the total number of ads in the page.
Next is the top.php file, this just has the straight adsense code within my left bar. I have the body code in the bottom.php file, but you will get the idea from the one example:
<div id="left">
<div class="ads">
<script type="text/javascript"><!--
google_ad_client = "pub-somepubID";
google_ad_width = 160;
google_ad_height = 600;
google_ad_format = "160x600_as";
google_ad_type = "text_image";
google_ad_channel ="<?php echo $google_ad_channel;?>";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0062BD";
google_color_text = "000000";
google_color_url = "FF3300";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div><!--closes .ads-->
</div><!--closes #left-bar-->
Notice for the channel number, we are using the <?php echo $google_ad_channel;?> code. This ties in with the ads.php file.
And finally the index.php file looks like this:
<?php
$page_title = "Title of my page";
include('articles/cat_articles.php');
include('ads.php');
include('top.php');?>
<p>Body content goes here</p>
<?php include('bottom.php');?>
--------------------------------------
So basically heres how it works:
The cat_articles.php include puts the $category = "articles_sky"; code into the page to let ads.php know that this page wants the channel numbers for the ads tracked with all of the article channels.
The ads.php lists every channel used across the board and matches up with request with cat_articles. The code:
if ($category=="articles_sky"){
$google_ad_channel = "987654321";
}
works with the cat_articles file passing back the google_ad_channel number. This is what gets plugged into the core adsense code for top.php through the beauty of php.
So using cat_articles.php and ads.php, the final output code of the index page looks like the intact code google originally outputs when you generate a new adsense ad with a channel ID:
<div id="left">
<div class="ads">
<script type="text/javascript"><!--
google_ad_client = "pub-somepubID";
google_ad_width = 160;
google_ad_height = 600;
google_ad_format = "160x600_as";
google_ad_type = "text_image";
google_ad_channel ="987654321";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "0062BD";
google_color_text = "000000";
google_color_url = "FF3300";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div><!--closes .ads-->
</div><!--closes #left-bar-->
Finally, since the cat_articles.php and ads.php files are includes that use pure php code, none of this info is written out in the parsed index.php file. Noone will see the code when they do a view source, it will look just as if you hardcoded it in the page by hand.
The benefit here is that if I want to try different ad layouts and experiement with stuff, I only have to make changes to the top.php file that has the core adsense code. The channel numbers will be the same, and even if they change, I only have to adjust the ads.php file. The index.php file should never have to be messed with again. And as I create hundreds of pages that are article or news based, I will track the channels related to them as a whole under Google's adsense control panel. Best of all, my template file with the includes predefined only needs one adjustment, and that is which cat_pagetype.php file to include (news, articles, reviews, etc). Once these are declared, they will never need to be changed.
Since the includes are so generalized and spread out, if I ever stop using adsense, or incorporate another type of ad format, whatever, it should be easy to do everything by only changing one or two pages.
I hope this all made sense. Since I am still very new to PHP, I limited in my explanation abilities and terms but if anyone has any questions, I will answer them best I can.
Thanks again to Storm for all the suggestions and code that essentially made this stuff work.