PPC & Media

Enhanced PPC Attribution with PHP

Attribution is absolutely vital for good PPC. Imagine if you set up a campaign through Google AdWords and in your reports there were fields for impressions, clicks, and cost, yet no way to view your conversions. What if when visitors converted on your site, you had no way to tell whether they were a direct visit, from another referring site, organic searchers, or pay-per-click visitors? You’d be dumping money into AdWords without any metrics to measure your ROI. That’s just not smart.

Thankfully, it’s easy to set up conversion tracking for Google AdWords. Just tag your conversion event and you’re set (I’ve over simplified this, admittedly). However, what if your business relies partially (or completely) on phone calls as the conversion event? You can’t put a conversion code on your website’s phone number and expect PPC visitors to click it. For the same reason, you can’t put messages on your landing pages telling visitors, “if you clicked on an ad to get here and need more information, call…” some PPC-dedicated phone number. Well, turns out you can; I’ve seen it done. When you really think about it, a phone number makes PPC attribution about as clumsy and random as a blaster.

Well, here’s a more elegant solution for a more civilized age: dynamic website headers. One of the most common locations for a website’s phone number is in its global header (the footer’s less common, so we’ll stick with the header).

Functionality that changes the global header based on the visitor’s source would be all you needed to attribute phone calls to PPC; just make sure you have one dedicated phone line for PPC so you know which source leads come from.

So we know the solution is dynamic headers, but how do we get there? PHP! If you’re website’s server is set up with PHP, you’re in luck. If not, the theory behind this method can be used with any server-side language. Here’s how we do it.

Break Out Your Global Header

If your website is built using static HTML, you’ll have some adjustments to make. You need to break out your global header from the rest of the page. Doing this is actually one of the great things you can use PHP for. While your page content changes page-to-page (if it doesn’t change, you have an extremely boring site), certain elements always the same: the navigation, the footer, and especially the header. While I don’t have the time or space to get into a full PHP lesson here, what you’ll essentially need to do is call in your global header using the “includes” PHP function instead of embedding the HTML on the page.

The file for the header (header.php) can live in an “includes” directory on your server. The file itself can just be the raw HTML that was once in embedded on every page of your site. A nice side benefit is that if you want to change anything in your header in the future, you only need to do it in one place. Neat, huh?

Click for a closer look at the code

Fortunately, many sites already use this functionality. WordPress does it right out of the box. If you’re site doesn’t have this set up already, it’ll take a little bit of work, but it’ll be worth it in the long run.

Create a PPC Global Header

We want the same header with the same phone number to be displayed everywhere on the site, except when a visitor comes from PPC; in that case, we want them to see the PPC-dedicated phone number only. To do that, we first need to create another header file in the “includes” directory that uses the PPC-dedicated phone number instead of the normal one. Just copy your header.php, rename it ppcheader.php and update the phone number. That’s all we need to do for now. We’ll build out some neat code to call that in at the appropriate time later on.

Use a Query String Parameter

Before we get into actually calling the special PPC header we just made, let’s figure out how we are going to send the message to the site that a visitor actually came from PPC. A simple way is to use a query string parameter. You’ve no doubt seen these in use before. They look something like this:


At this point, all you need to do is decide on your parameter name and the value. Later on, we’ll check to make sure the parameter exists and matches the correct value before returning the PPC header.

For simplicity, I like to set the parameter as PPC and the value as TRUE.


Check for the Parameter and Set a Cookie

Ready for the code-heavy section? Like I said previously, this can’t be a full on PHP lesson, but I’ll try to explain the theory behind each piece of code.

To keep things organized, the code we build out in the following steps should live as its own file in the “includes” directory. Let’s name the whole thing campaignTracking.php. I’ll explain how and where to call this code into your page later on.

The first thing we’ll want to do is check if the PPC parameter equals TRUE.

if($_GET[“ppc”] == “true”)

This snippet uses the $_GET super global (you don’t need to know what a super global is) to grab the PPC parameter and check if it is equal to “true”. If it that checks out, then we want to tell our website that, “yes, this person came from PPC”. That’s where the cookie comes in. A cookie is a bit of info stored in a visitor’s browser that we can reference to call the appropriate header. This also lets us display the PPC header on every page they visit throughout the site since it’s stored in their browser and essentially follows them around.
The value of the cookie we set is arbitrary, but let’s make in easy to understand and make it equal to “sourcePPC” so we know that the “source is PPC”.

$cookieValue = “sourcePPC”;

After we define the cookie’s value, we can use PHP’s “setcookie” function to set a cookie in the visitor’s browser.

setcookie(“SwellPathCampaignTracking”, $cookieValue, time()+60*60*24*90);

The line above sets a cookie named “SwellPathCampaignTracking” equal to our specified cookie value and makes it last for the next 90 days. The setting the expiration date 90 days in the future, we can display our PPC header for that same visitor even if they leave and come back. Essentially, we want to account for people discovering the site through PPC, leaving, and then coming back to convert via phone.

Finally, we want to set something that’s easy to use when we want to call in our PPC header. We’ll call our variable “cookie” and make it equal the same thing as “cookieValue” (which is “sourcePPC”).

$cookie = $cookieValue;

Remember that we only want everything above to happen if our PPC parameter is TRUE. So, we’ll wrap that all up within an ‘if’ statement.

if($_GET[“ppc”] == “true”)

$cookieValue = “sourcePPC”;
setcookie(“ReboundCampaignTracking”, $cookieValue, time()+60*60*24*90);
$cookie = $cookieValue;


The last thing to address, before we move on, is to allow the cookie variable to be set regardless of what page the visitor navigates to (provided they came from PPC).

else {

$cookie = $_COOKIE[“SwellPathCampaignTracking”];


The statement above sets the cookie variable to whatever is stored in the visitor’s browser as SwellPathCampaignTracking. If they’ve visited the site before via PPC, they’ll have that cookie set and the cookie variable will be set correctly. If it’s a visitor from direct, referral, or organic sources, they won’t have a CampaignTracking cookie stored in their browser and the cookie variable will be empty.

Completed code:

Click for a closer look at the code

The Switchup

Now that we have all that deep coding out of the way, we can do the actual fun part and write our code that will switchup the header. We’ll do this based on the value of the cookie variable.

Remember when we broke out our global header into header.php and ppcheader.php? This section would suck without it. Basically, if the cookie variable equals “sourcePPC” we use ppcheader.php when PHP builds the page. If it doesn’t equal that or isn’t set, we use the standard header.php. Again, for organization, keep this code in the “includes” directory as its own file. Call it switchup.php

Click for a closer look at the code

Putting it all Together Like A Boss

Now that you’ve built out all your code (like a boss), it’s time to put it all together to make a page.

Note that in order for this whole thing to work, campaignTracking.php needs to fire before anything else. If you don’t check for the parameter and set the cookie before PHP builds the rest of the page, it’ll be too late and you won’t get to pull in the cool PPC header. TL;DR: make sure to include campaignTracking.php before the first tag.

Just so there’s no confusion, remember that the of an HTML document and the header are not the same. The section contains info about the page and calls in other cool stuff like JavaScript and CSS. The header is a piece of the visible, user-facing site that will always be within the tag.

Click for a closer look at the code


Finally, make sure that you use this layout on every page of your site that you want to enable the PPC-dedicated phone number on. It’s not uncommon to have a different PHP template for your custom PPC landing pages than the one used on the rest of your site. If you have a blog, it likely has it’s own PHP template as well that’ll need to be updated.

Making it Work with Your PPC Campaign

You barely need to change anything in your PPC campaigns to make this work. Simply append “?ppc=true” to your landing page URLs when you build your ads. (If you already use query string parameters, just add &ppc=true).


When a PPC visitor clicks through on one of your ads, all of our awesome PHP code with execute and return the PPC header with the PPC-dedicated phone number. Since we set a persistent cookie, the PPC header will be displayed on every page they visit and not just the first landing page they hit.

Bam! Enhanced PPC Attribution with PHP, or EPPCAwPHP, if you want to be cool.

Follow Mike Arnesen on Google+

Mike Arnesen

Mike Arnesen - Director of Analytics & Optimization

A diehard SEO and web analytics geek, Mike is the Director of Analytics & Optimization at SwellPath. He is also a board member at SEMpdx. Mike's fascination for search experience optimization, structured data and semantic markup, and web technology knows no bounds. Beyond geeking out with SEO and analytics, Mike is also a prolific blogger, speaker (MozCon, SemTechBiz, SEMpdx, SMX, State of Search Conference, etc.), and company culture advocate. When not in the office, Mike is spending time with his wife, enjoying the outdoors, or keeping up with inbound marketing news via mobile; most of the time, it's all three simultaneously.

Watch Mike talk about his role and life at SwellPath

16 Responses to “Enhanced PPC Attribution with PHP”

  1. Adam Ware

    Good code + solid graphics = all-star post!

  2. New PHP Coding Project | Mike Arnesen

    […] The most complicated thing I’d done in the last year was building some functionality to change a PPC phone number using PHP in a site’s global header. So, about two weeks later, I’m about done with the tool. It […]

  3. John

    What if I have multiple phone numbers that I want to change based on URL parameters? I have tried using your code to create arrays, switch statements, etc. but either the persistence breaks or it stays with the default #. Any ideas would be greatly appreciated.

    • John

      Nevermind, I figured it our, I needed to set the array for when it checks for cookies correctly. Final code looks like this:

      • Mike_Arnesen

        Hey John,

        I’m so glad you found the post useful. I’ve never had a case where the client needed multiple phone numbers for different variables, but it sounds like a fun challenge.

        Looks like the code in your comment got stripped out. Mind posting it up on gist.github.com or similar? I’d love to check it out. 😉

  4. Sarah Cowan

    Creating a new site redesign on a testing server. This is just what I’ve been looking for but it always seems to default on the main phone number for me even when i add ?ppc=true. Must go through and double check everything. I thought I had it spot on…

  5. Sarah Cowan

    Ok… so… I’m obviously not boss enough. lol
    I just can’t get this to work? Would you by any chance be able to look at it for me? I think the problem is lying in campaignTracking.php as I don’t see the cookie registering at all?
    Code for campaign tracking

    • Mike Arnesen

      Can you post the php files you’re using up at https://gist.github.com/ ? I’d be more than happy to take a look at them and see if I can figure out why the cookie isn’t being set.

      Also, if you test it in Chrome, visit a URL with the ppc=true parameter and then go here chrome://chrome/settings/cookies. Search for your domain name and make sure the cookie has actually been set.

      Anyway, keep me updated. Twitter may be easier. @mike_arnesen

  6. purplesars11

    Still no joy getting this to work. Does anyone know of anything similar on another site?

    • purplesars11

      Glad to finally get this working. It’s perfect for what I need. Also works with mulitple paramater values and phone numbers using elseif statements :)


Leave a Reply

Loading Facebook Comments ...

Get Our Newsletter

To keep up with the latest news from SwellPath on digital advertising, analytics, and SEO, sign up for our newsletter.