Google Adwords Dynamic Keyword Insertion PHP Script
So, you want to be an Adwords rockstar?
Well then, you'll need to wrap your brain around dynamic keyword insertion.
And I'm not just talking about the basics - just using dynamic keyword insertion in your Google Adwords advertisement. I'm talking about playing with the big kids and using those keywords that appear in your Adwords advertisement also on your web page.
That's right, if you are targeting a specific search terms in your advertisement - make that keyword show up on your landing page. Heck, even have it show up as the title and actual url of your page. Now we are talking.
Maybe instead of talking, let me show you a few things to give you the picture. It will all be crystal clear in just a moment ...
What Is Dynamic Keyword Insertion?
Lets say you are advertising using Google's Adwords program. One of the things you provide for your ad is a list of keywords that you are interested in having your advertisement display for.
For instance, if you were selling "kobe beef", you might want your advertisement to show up on Google's search engine page every time someone types in the words "kobe beef" in the search box on Google.
Same goes for someone searching for "kobe steak" - you want your ad displayed.
With dynamic keyword insertion, every time someone types in the exact search term you are targeting, your advertisement would include those keyword - in bold - on your advertisement.
Here's what the syntax looks like:
{keyword:} - will display the search term or not if there is not a match
{keyword: Default} - will display the search term or "Default" if there is problem displaying the search term. Of course, you can replace "Default" with whatever you like.
Some specific details regarding keyword: (source: Red Fly Marketing )
keyword - No capitalization, all word(s) are in lower case
Keyword - The first word is capitalized
KeyWord - Every word is capitalized
KEYword - Every letter in first word is capitalized
KEYWord - Every letter in the first word AND the first letter of the second
KEYWORD - Every letter is capitalized
More details at Google Adwords
How Do You Do It?
Login to your Adwords account. Create or edit a campaign. Create or edit a Text Ad.
Here's an example of what an ad might look like that uses keyword insertion.

Notice that use of the {KeyWord: Default} in the Headline and Description line 1.

This is the important part for getting the search term to show up on your web site. Use the {Keyword: Default} as part of your Destination URL. In this example, I have added my top level domain, http://www.DaveWooding.com/ followed by the keyword insertion expression followed by ".html". Note that I have added a dash, "-", in between my default word.

Display the Adwords Search Terms On Your Page
At this point, somebody clicked on an Adwords ad of yours, something that might look like this:

In this case, the search term is "kobe steak". A visitor that clicks on that advertisement would be sent to this url, http://www.davewooding.com/kobe%20steak.html.
If someone searched for "kobe beef" the advertisement would look like this:

On To Your Web Site
Now that you have setup your Google Adwords advertisement to use keyword insertion, it is time to separate the men from the boys, the women from the girls, the ... you get the point.
Time to play big. You want your site to display an appropriate page for each of those particular search terms. Sure you can do it the old fashion way and grunt it out - create an individual page for each and every possible search term.
Or you can do it the right way.
Using PHP or some other scripting language that allows for extracting information about the referring page - the referring page that uses dynamic keyword insertion. Ding, ding, ding <<<--- important
First, assuming you are working from the top level domain, i.e. - http://www.example.com, you will need to add or modify a .htaccess file to include the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+).html?$ index.php?q=$1 [L]
I'm going to assume that if you are reading this far into the article that you are willing to use php files instead of plain html file (otherwise you will need to include additional code in your .htaccess file, specifically "AddType application/x-httpd-php .htm .html").
Very simply, the chuck of code above says to turn on the rewrite engine, reference to the root directory, the rule that follows is applicable if the file or directory referenced does not already exist, then grab all the info after the domain but before the .html extension <- this is our dynamic keyword insertion stuff courtesy of Google Adwords.
Take that information (the dynamic keyword info), shove it into memory - which we will call "dollar one" = $1 and send it over to the index.php file as part of the "q" query string.
You like that techo jargon?
The PHP Code That Displays Adwords Search Terms
... on your site.
As I mentioned, you will need to be using php files for this to work (or you can modify your .htaccess file to accomodate).
At the top of your index.php file, the code should like like this:

This chunk of code checks to see if any information has been passed over via the "q" query string (remember the .htaccess grabbed any info between the top level domain and the .html and sent it over as part of the query string parameter).
If there is info, then that information gets assigned to a variable called $replace (after it replaces any dashes with whitespace - assuming there are any dashes).
If no query string info is present, no problemo, there is a default value set previously - "kobe beef" in this case.

Next a few other variables are set based on the what is in $replace. $title is the same as $replace with the exception that the first character of every word are capitalized. $h1 is the same as $replace except that just the first character is capatilized.

Following the php script part of the index.php file is plain old html code - with the $title and $h1 variables thrown in.

This is how you display the Adwords search terms on your site.
What's Next?
We are only scratching the surface of what is possible here.
Lets just speculate that you have a datafeed that you want to create individual landing pages for that you want to use PPC to drive visitors to. Or you are a Clickbank affiliate that wants to use Adwords to promote products and you want to use the Clickbank datafeed? Or you have an Ebay store that uses a RSS feed from Ebay to display items for sale that could afford pay per click marketing?
What you have seen here is perfect for creating relevant, high quality score landing pages for using Google Awords with.
Stay tuned, or sign up to get on the mailing list, for the next article which will show you how to integrate a datafeed - like SpeedPCC - with dynamic keyword insertion.







September 11th, 2008 21:27
An alternative to using the .htaccess is using a custom 404 error page to do url rewriting. The premise is simple...
1) your adwords destination url points to a non-existant page on your website.
2) your custom 404 error page is called, and it deconstructs the keyword from the url
3) the 404 displays the appropiate content or redirects to a page that does the same
November 14th, 2008 15:23
Hi,
This is a really cool post! Full of possibilities.
For some reason, I couldn't get it to work though. I broke it down into pieces. Before putting a dynamic keyword in my Google ad, I wanted to test the concept.
First I created a page called survey.php in my root directory. Then I added the above code to my .htaccess, changing index.php to survey.php. This is what I put into my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+).html?$ survey.php?q=$1 [L]
Then I pointed my browser to:
http://www.mydomain.com/blah.html
I was expecting to see my survey.php page with ?q=blah
but instead I got a 404:
The requested URL /blah.html was not found on this server.
The error tells me that the code in my .htaccess isn't working.
Can you shed any light on why it didn't work?
Thanks.
November 14th, 2008 15:30
Ha ha ha!!!
Ignore my previous post. It would help if I actually saved my changed .htaccess before I uploaded it!!
November 15th, 2008 08:21
Hi Susie,
Welcome to my world :)
Dave
November 16th, 2008 10:41
Hi,
Another question.
In my case I want the visitor to go to a survey page before continuing to the site. The survey page will use the keyword in its content, but then I want to redirect the user to different pages.
I tried to modify the above so that I can pass the redirect page as a query variable. But being so new with Regex, it isn't working.
Here's what I tried:
RewriteRule ^([^/]+)\.html\?RD=([^&]+)$ survey.php?q=$1&rd=$2 [L]
Even though I tested the expression with an online tester and it matched and gave me the group values I expected, when I put this into my htaccess and tested it with:
http://www.mydomain.com/blah.html?RD=123
I got a 404 that it couldn't find blah.html. And yes, I did make sure I uploaded the changed htaccess this time :)
Any advice?
Thanks, Susie
November 19th, 2008 13:51
Aha, I got my answer.
Query strings in url redirects are special cases.
So now this is what I have and it works:
RewriteRule ^([^/]+)\.html$ survey.php?q=$1 [QSA,L]
The QSA flag tells it to send through whatever query string it had originally so the end result is that when I put this into the browser:
http://www.mydomain.com/blah.html?RD=123
It redirects to this:
http://www.mydomain.com/survey.php?q=blah&RD=123
November 19th, 2008 13:51
One last question (I hope).
Is it better to have the redirect be a 301 or a 302, or doesn't it matter as far as Google is concerned?
Thanks, Susie
November 21st, 2008 17:21
Hi Susie,
Sorry about not participating in the conversation ... I don't have a good answer for your regarding 301/302.
Dave