Forum Moderators: coopster

Message Too Old, No Replies

Changing phone number based on referrer

         

sicsiksix

1:28 pm on May 12, 2010 (gmt 0)

10+ Year Member



Hi,

I've recently taken over running a website for a company and they have a piece of code which changes the phone number based on where the visitor came from, and includes the source and keyword information when a visitor submits a form.

Here is the current code (modified for anonymity)

session_start();
if(!isset($_SESSION["phonenumber"]))
{
if(isset($_SERVER['HTTP_REFERER']))
{
//not direct, must be PPC or Natural
if(isset($_GET["gcid"]))
{
//must be PPC
$_SESSION["keyword"] = $_GET["keyword"];
if(isset($_GET["gclid"]))
{
$_SESSION["phonenumber"] = "00000000000"; //PPC
$_SESSION["keyword"] = "GOOGLE PPC -> " . $_SESSION["keyword"];
}
else
{
if(strpos($_SERVER['HTTP_REFERER'],'yahoo') > 0)
{
$_SESSION["phonenumber"] = "00000000000"; //PPC
$_SESSION["keyword"] = "YAHOO PPC -> " . $_SESSION["keyword"];
}
else
{
$_SESSION["phonenumber"] = "00000000000"; //PPC
$_SESSION["keyword"] = "MSN PPC -> " . $_SESSION["keyword"];
}
}
}
else
{
//natural
$_SESSION["phonenumber"] = "00000000000"; //NATURAL
$referer = "";
if(isset($_SERVER['HTTP_REFERER']));
$referer = $_SERVER['HTTP_REFERER'];
$_SESSION["keyword"] = "Natural -> " . $referer;
}
}
else
{
// direct
$_SESSION["keyword"] = "Direct";
$_SESSION["phonenumber"] = "00000000000"; //DIRECT
}
}

What I am wondering is how to add four additional entries to this for 4 different numbers:
- Traffic redirected from www.example.com (forwarded domain)
- Traffic redirected from www.example2.com (forwarded domain)
- Traffic to www.ourdomain.com/specificpage1.php
- Traffic to www.ourdomain.com/specificpage2.php

All links coming to the website using one of these 4 options will be using google analytics tagged links (including domain forwards), so will contain common parts of the URL if it can be changed using tags like ?utm_source="oursource"

I'm still new to PHP and while I understand what this piece of code is doing, I don't fully understand how it works so would appreciate any guidance people could give me.

Thanks,
Sicsiksix

jatar_k

1:43 pm on May 12, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



first thing is that the code you posted won't work, there are double 'else' conditions on 2 'if' statements, it will only ever evaluate a single 'else', though which one could change and I have no idea if it takes the first or final occurrence.

You could just switch those to 'else if' but probably switch [php.net] statements would help make it more extensible, which is what you are currently looking for.

frankly, I am not sure what that code is trying to achieve, I understand it is to switch phone numbers based on criteria. You might want to write out in common language what the criteria/flow is. It definitely is not working as desired.

You also might want to look at using keywords or custom urls in your ppc campaigns to make tracking easier as opposed to using referer, which is unreliable.

sicsiksix

2:03 pm on May 12, 2010 (gmt 0)

10+ Year Member



All If statements only contain one else, it's just not very clear when written without the whitespace leading each line, there are up to 5 nested IF statements I think.

What this code currently does is changes the phone number and the keyword information.

If a customer comes from google adwords, a specific phone number will be displayed at the top of the website and the keywords used will have "Google PPC ->" added prior to them, so if a user submits an enquiry form to us, we can see in the email where they came from (Google PPC) and what keywords they used to do so (Google PPC -> Keywords here)(if applicable).

Same with Yahoo PPC, a different number and keyword tag (or whatever you wish to call it), and so on.

The code does currently work as far as I'm aware, but like I said, I understand what it does, just not fully how it does it.

If you think there would be an easier way to achieve the same goal, please advise on how to go about it. I'm new to PHP but I do understand most things after a little research.

Edit:

Currently this code does the following:

Checks for a referrer
- If There is a referrer the traffic must be PPC or Natural traffic
-- Checks for gcid (tag on link?)
-- Must be PPC traffic, if there is a gclid tag ? must be Google
--- Set google number + keyword tagging
-- If Not google, must be yahoo or msn
--- Checks if referrer is yahoo
---- Sets Yahoo phone and keyword tagging
--- Not yahoo, must be MSN
---- Sets msn phone and keyword tagging
-- If not PPC must be natural
--- Set natural phone number and tag keywords with natural
- No gcid tag so must be direct
-- Set direct number and as no keywords, set keywords to direct

sicsiksix

2:18 pm on May 12, 2010 (gmt 0)

10+ Year Member



We are using Analytics and our newest PPC campaign is using custom URLs so we can track them, but this doesn't change the phone number on the website which we would like to do. while referer isn't always accurate, it's better than nothing.

And it has also been requested that the information is provided within the enquiry form emails.

Could I add to the above code to check for utm_source or similar and change the number that way? If so, how would I go about doing that?

jatar_k

2:59 pm on May 12, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



wow, I sure messed that up, and I actually looked at it quite a bit, there was one error

if(isset($_SERVER['HTTP_REFERER']));
$referer = $_SERVER['HTTP_REFERER'];

assuming this is an inline if it shouldn't have the semi colon after the if

you can add the session vars to emails easily enough just by grabbing the data from the session and echo'ing it into the message

as far as adding extra stuff into your little construct here, you would just need to add to your common language explanation (nice by the way) and code it into the same place.

You might want to add the full referer to the session every time, not just on natural, it might help diagnose any inconsistencies.

sicsiksix

2:02 pm on May 13, 2010 (gmt 0)

10+ Year Member



Ok... so with the additional requirements it would look something like this:

Checks for a referer
- If There is a referer the traffic must be PPC, Banner or Natural traffic
-- Checks for gcid (tag on link?)
-- Must be PPC traffic, if there is a gclid tag ? must be Google
--- Set google number + keyword tagging
-- If Not google, must be yahoo or msn
--- Checks if referrer is yahoo
---- Sets Yahoo phone and keyword tagging
--- Not yahoo, must be MSN
---- Sets msn phone and keyword tagging

-- If not gcid tag, check for utm_source tag
--- check if utm_souce=ExampleSource1
---- Set phone and keyword tagging for ExampleSource1
--- if not ExampleSource1 check if utm_source="ExampleSource2
---- Set phone and keyword tagging for ExampleSource2

-- If no gcid or utm_source tag, must be natural
--- checks if referer is example.com
---- sets phone and keyword tagging for example.com
--- checks if referer is example2.com
---- sets phone and keyword tagging for example2.com
-- not a specific site
--- Set natural phone number and tag keywords with natural

- No referrer so must be direct
-- Set direct number and as no keywords, set keywords to direct


How would I go about checking for a specific tag and what that tag is?

From the existing code, I would imagine it would be something like:
if(isset($_GET["utm_source"]))
and then how would I check it's value?

Also, do 301 forwarded domains register as a referer when directly entered into a browser?

jatar_k

4:33 pm on May 13, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



session_start(); if(!isset($_SESSION["phonenumber"])) { if(isset($_SERVER['HTTP_REFERER'])) { //not direct, must be PPC or Natural if(isset($_GET["gcid"])) { //must be PPC $_SESSION["keyword"] = $_GET["keyword"]; if(isset($_GET["gclid"])) { $_SESSION["phonenumber"] = "00000000000"; //PPC $_SESSION["keyword"] = "GOOGLE PPC -> " . $_SESSION["keyword"]; } else { if(strpos($_SERVER['HTTP_REFERER'],'yahoo') > 0) { $_SESSION["phonenumber"] = "00000000000"; //PPC $_SESSION["keyword"] = "YAHOO PPC -> " . $_SESSION["keyword"]; } else { $_SESSION["phonenumber"] = "00000000000"; //PPC $_SESSION["keyword"] = "MSN PPC -> " . $_SESSION["keyword"]; } } } else if (isset($_GET["utm_source"])) { if ($_GET["utm_source"] == 'ExampleSource1') { $_SESSION["phonenumber"] = "00000000000"; $_SESSION["keyword"] = "utm_source -> ExampleSource1"; } else if ($_GET["utm_source"] == 'ExampleSource2') { $_SESSION["phonenumber"] = "00000000000"; $_SESSION["keyword"] = "utm_source -> ExampleSource1"; } } else { //natural $_SESSION["phonenumber"] = "00000000000"; //NATURAL $referer = ""; if(isset($_SERVER['HTTP_REFERER'])) $referer = $_SERVER['HTTP_REFERER']; $_SESSION["keyword"] = "Natural -> " . $referer; } } else { // direct $_SESSION["keyword"] = "Direct"; $_SESSION["phonenumber"] = "00000000000"; //DIRECT } }

you'd have to test for the forwarded domains

sicsiksix

11:09 am on May 17, 2010 (gmt 0)

10+ Year Member



Thanks a lot by the way :)