Tracking your inquiries with a bit of coding

The place to discuss anything to do with computers, software, hardware, no matter how basic or technical. We all use this stuff, but we don't always understand it!
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Tracking your inquiries with a bit of coding

Post by vrooje »

Even with a nice webstats package it's usually not possible to tell for sure where a particular inquiry from your personal website comes from.

You can ask the client, but they won't always remember, and if they just say "Google," how do you know they didn't Google to find thislistingsite.com, through which they found you, bookmarked you, told their spouse, and came back a week later to submit an inquiry? And what if you cancel your membership to thislistingsite.com because you don't think the site is performing well, when in fact 50% of your personal-site inquiries were click-throughs from that site? You'd lose bookings because you didn't have all the facts.

I think I've found a good way of tracking the original source of inquiries that came through my personal website. I've never shared the details of it here, but recently I've been asked, why not? And I couldn't think of a good reason why not -- so I thought I'd share, in case it helps someone.

It involves adding bits of code to your website, but it's not super-advanced coding or anything. Basically, you set a variable whenever someone enters your site (called a cookie), which stores the place they came from -- be it Lay My Hat, YHM, VRBO, Google, whatever. Then they can browse your site to their heart's content, and if they do decide to submit an inquiry (even days later), you include the variable with the inquiry, and the inquiry e-mail tells you how the client found you. And it does this without the need for the client's input, so there's no human error factor involved.

It's hard to give detailed instructions on this because it can be done so many different ways. My website is in PHP, but it can be done in any scripting language, or even plain HTML with the use of Javascript. But I can give a general step-by-step for what happens on my website:
  • 1. Every listing site I am on which allows a link to a personal website doesn't just link to my home page (http://www.experienceburgundy.com/index.php). They link to my homepage with the URL adjusted to include a variable, for example http://www.experienceburgundy.com/index.php?ref=thislistingsite. The variable is just the name of the listing site, like "VRBO" or "HolidayLettings".

    2. When anyone clicks through to my home page, I have a bit of code which grabs the "ref" variable and saves it to a cookie. If the code can't find the "ref" variable, it just saves the referring URL in the cookie. All of which only happens if the cookie doesn't already exist (so that if this is the client's second visit to the website, it wouldn't overwrite the data saved during their first visit).

    3. If the client decides to submit an inquiry through my website, there's a little piece of code on the inquiry page which grabs the value of the cookie and inserts it as a hidden item (so that it submits along with the rest of the form, but without the client having to see it).

    4. If the client decides not to inquire, the cookie will just expire after a set period of time (I set it for 30 days). But if they come back to the site before the cookie has expired, the cookie won't be fooled. So if someone did find my site initially through thislistingsite.com, but didn't inquire until they'd gone home, talked about it with their spouse, and then found me again with a Google search, the cookie would still tell me the inquiry initially was a click-through from thislistingsite.com.
It was initially a bit of work and testing to get this up and running, but now that it is, I'm very glad I did it, because (with very few exceptions) I can now say that I absolutely know which of my personal-site inquiries were actually click-throughs from a listing site. That makes the listing site happy (they get credit where credit is due), and me happy (because I know which of my listing sites really work, and how well my own site is doing at convincing people to inquire once they've clicked through).

Just figured I'd post this for informational purposes (sorry, I know it's long)... there has been a lot of recent discussion on webstat packages, and those are great (I use Google Analytics and wouldn't want to go without it), but even those can't tell you everything. :)
Brooke
la vache!
Posts: 11065
Joined: Wed Feb 16, 2005 7:22 pm

Post by la vache! »

I've just posted on another thread, but at the risk of repeating myself, I've been using Brooke's cookie program for over 6 months and it works for me. Bookings that I'd previously credited to google for coming direct to my site, I can now track to the relevant rental listing site. I've never been able to get this info from Statcounter or Awstats, but Brooke's program has really helped me know which listing sites to keep and which to ditch.
Cheers Brooke :wink:
slartibartfast
Posts: 87
Joined: Thu Aug 02, 2007 7:12 am
Location: Ovacik , Olu Deniz, Hisaronu , Fethiye ,Turkey ..but live in Perth
Contact:

Post by slartibartfast »

That sounds very useful ... if I knew how to do it :wink:

I am on MrSite which doesn't have PHP so HTML /Javascript would work. Is it too cheeky to ask for some sample code or should I just RTFM ? :oops:
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Not cheeky at all! :) Perhaps on a coding forum people might tell you to RTFM (actually I think it's standard practice on many coding forums), but not here!

The issue with code samples is that I can't verify that any code sample works except a PHP code sample -- because that's what I use. I can PM you with a javascript code sample that I've put together from several websites:

http://www.w3schools.com/js/js_cookies.asp
http://javascript.geniusbug.com/index.p ... e=passVars

Those are the main two (I used others to get some syntax right, I'm really not a javascript coder), and then I wrote a couple of functions to put them together. I'll PM you with the details... I just hesitate to post the code yet because I haven't tested it. You could certainly be the test case! :)
Brooke
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

vrooje

if the html/javascript code worked or was never tested please could you let me know I would be very interested and do not have the ability to work it out for myself

pete
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Hi Pete,

Sure... I know that a couple of forum users have tried the JS form of the code, but I don't know for sure that it works perfectly (I use the PHP form so that's the only one I can really vouch for).

However, I will copy-paste my instructions here:

1. Create a file called manage_cookies.js. I'm going to assume you'll put it in the same directory where your web pages are stored. (Of course you can name it whatever you want, but I'm going to assume it's called the above.)

Into that file, copy this:

Code: Select all

// global variables
var cookiename = "referring_site"
var cookiedays = 30  // number of days cookie will be saved


// general cookie functions
// from http://www.w3schools.com/js/js_cookies.asp

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
    }
  }
  return ""
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date()
  exdate.setDate(exdate.getDate()+expiredays)
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}





// function to get variables passed through the URL
// from http://javascript.geniusbug.com/index.php?action=show&name=passVars

function getVars()
{
   var tmpUrl = document.location.href.split('?') ;
   var varArray = new Array() ;
   if (tmpUrl.length > 1) {
      varArray = tempUrl[1].split('&');
   }  else  {
      varArray[0] = "ref=NoRef" ;
   }

   for&#40;var x=0; x<varArray.length; x++&#41;
   &#123;
      var tmp = varArray&#91;x&#93;.split&#40;'='&#41; ;
      eval&#40;unescape&#40;tmp&#91;0&#93;&#41; + '="' + unescape&#40;tmp&#91;1&#93;&#41; + '"'&#41; ;
   &#125;
&#125;


// function to run when index page loads;
// retrieves and sets cookie value

function getRef_setCookie&#40;&#41; &#123;

  // get "ref" variable &#40;should be passed through URL&#41;
  getVars&#40;&#41;

  // if cookie is not yet defined, set it
  if &#40;getCookie&#40;cookiename&#41; == ""&#41; &#123;

    // if "ref" variable is defined, use that
    if &#40; typeof&#40; window&#91; 'ref' &#93; &#41; != "undefined" &#41; &#123;
      setCookie&#40;cookiename, ref, cookiedays&#41;
   
    &#125; else &#123;
    // if "ref" variable isn't defined, use the referring URL
      setCookie&#40;cookiename, window.document.referrer, cookiedays&#41;
   
    &#125;


  &#125; // if cookie is already defined, don't do anything

&#125;




// function to run when loading inquiry page

function sendReferrer&#40;&#41; &#123;

  // get the value of the cookie
  var ref_value = getCookie&#40;cookiename&#41;


  document.EnquiryFormName.referring_site.value = ref_value

&#125;
This contains all the code you should need. Note the place where the code references something called "EnquiryFormName" -- I'll refer to that later.

2. Go to your index page (or whatever page you have listing sites enter on) and add the reference to the .js file between your headers:

Code: Select all

<SCRIPT LANGUAGE="JavaScript" SRC="manage_cookies.js" type="text/javascript"></SCRIPT>

That assumes your .js file is in the same directory as your web page.

I put the cookie code on all the main pages of my site, because even if the client enters from a search engine onto, say, my house photos page and never views my homepage, I still want to set the cookie. So in that case you'd need to insert that tag into the headers of all your pages.

Also, put that tag into the header of your contact/inquiry page.

3. On your index page (and any other pages you'll be using this script, except your inquiry page), change your <body> tag by inserting the text

Code: Select all

onLoad="getRef_setCookie&#40;&#41;"
For example, if your body tag is a simple one that reads

Code: Select all

<BODY BGCOLOR="#FFFFFF">
you will need to change it to read

Code: Select all

<BODY BGCOLOR="#FFFFFF" onLoad="getRef_setCookie&#40;&#41;" >
That will tell the web browser to try and set the cookie each time the page loads. And the way the "getRef_setCookie()" function works, it will only set the cookie the very first time, and any other time it will do nothing.

4. On your inquiry/contact page, you'll need to add a few bits of code.

a. First, you will need a name for your inquiry form. I've used "EnquiryFormName" in the example. The name can be anything you want; but be sure it doesn't have spaces in it. The form name is set in the FORM tag, e.g.

Code: Select all

<FORM NAME="EnquiryFormName" ACTION="blahblahblah.php" METHOD=POST>
Whatever your form name is, replace "EnquiryFormName" with that in all the code above.

b. Then, add a hidden tag:

Code: Select all

<input type="hidden" name="referring_site" value="">
The value is now empty, but you'll use javascript to fill it.

c. Then, in the body tag, add the following:

Code: Select all

onLoad="sendReferrer&#40;&#41;"
as described in #3.

That code will fill the form with the cookie value once the page loads, so that when they send the form, it will be sent along with all the other information.

And then there's the last step:

5. Change your listing site info so that your links add the "ref" variable. So if you advertise on, say, Holiday Lettings, you'd go to their management area and change your link from

Code: Select all

http&#58;//www.yourwebsite.com/index.html
to

Code: Select all

http&#58;//www.yourwebsite.com/index.html?ref=Holiday_Lettings
and at Owners Direct you'd want to change the end to ?ref=Owners_Direct and so on for all your listing sites.

Note that in step #4, I'm assuming your mail form will allow you to send any input you like, including an input with the name of "referring_site". If it will only let you send certain information like e-mail addresses and comment fields, then you'll have to find another way (and you could, for example, append the text to the comment field instead, if we had to).

Soooo.... if you're up to it, give it a try and see how far you get before something returns an error or just doesn't do what it's supposed to.

(edit: 2 code fixes)
Last edited by vrooje on Tue May 20, 2008 7:52 pm, edited 2 times in total.
Brooke
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

vrooje

many thanks for posting this, I will start to do this this evening

pete
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

Brooke

I suppose the good thing is that I have managed 1 2 and 3!

To complete 4, my problem is that my code does not have a 'form name' - do you mean I have to create one or does my form just not have one and I therefore need to do something else? I have used an enquiry form from a free form internet site,

pete
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

pete,

Yep, that's right -- you should give your form a name. For example, it looks like your inquiry form says

Code: Select all

<form method="post" action="http&#58;//www.emailmeform.com/fid.php?formid=69715" enctype="multipart/form-data">
And you can name the form with

Code: Select all

<form name="EnquiryFormName" method="post" action="http&#58;//www.emailmeform.com/fid.php?formid=69715" enctype="multipart/form-data">
I've used the same example form name that I used above. Naming the form shouldn't affect the form's functionality.

I have a question: does your e-mail form handler allow you to add any form fields you like? What I mean is, if you add the field described in #4b. above, does the field get sent to you along with everything else in the inquiry form?
Brooke
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

I have loaded all the code, and changed one of the sites I list on to test but when I load my site I get a run time error for one of the new lines - I am working my way through this - and should have a test run later

pete
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Great, keep us posted!

I wouldn't be surprised if there is a typo somewhere in the code -- if you let me know where it is, I'll fix it so that future people won't have to do the same debugging when they copy-paste. :)
Brooke
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

Brooke

its line 1 and line 8 - where it inserts in the body - and it will be the weekend before I can test it so will let you know next week - and many thanks for all your help

pete
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

The first issue I see is in manage_cookies.js -- you should delete the html and body tags (the first two lines of the file and the last two lines of the file). That isn't an HTML file, so you don't need those tags.

You also have an extra HTML tag in your code on your contact page... in general, you want your HTML file to only have one HTML tag, and everything on the page goes in between the HTML and /HTML tags. If you have two, the browser may dislike it, though it may also just ignore it.

Also: I noticed your hidden input is

Code: Select all

<input type="hidden"name="referring_site" value="">
when it should be

Code: Select all

<input type="hidden" name="referring_site" value="">
The only difference between them is the space between "hidden" and "name" in the tag...

Something similar is going on in the code of your index page... the body tag has

Code: Select all

vlink="#3300CC"onLoad="getRef_setCookie&#40;&#41;"
where there should be a space before "onLoad".

Hope that helps? :)
Brooke
User avatar
pete
Posts: 1109
Joined: Sat Jan 19, 2008 6:40 am
Location: Near Sancerre, Loire Valley
Contact:

Post by pete »

Brooke

sorry to be the pain in the ....

the first runtime error now is from the onLoad="getRef_setCookie()" - if I remove this it does not happen - this is on the index page

and the second is line 40 on the js page - I have removed the first code for now so you may be able to see the line 40 syntex error - is it possible to adjust ?

many thanks

pete
User avatar
vrooje
Posts: 3202
Joined: Thu Dec 09, 2004 2:48 am
Location: Burgundy, France

Post by vrooje »

Ah! The Firefox Error Console took me straight to it.

Go to the function getVars() in manage_cookies.js and change the else statement line from

Code: Select all

   else  &#123; 
to

Code: Select all

   &#125;  else  &#123; 
My oops!

My guess is that if you fix that, the other will resolve itself. The error on the index page was probably because the missing bracket was messing up the rest of the js file...

...but of course there's no guarantee that there aren't any errors below line 40 in the js file! :)
Brooke
Post Reply