views:

353

answers:

1

Can i track the http referer with javascript, and append a variable to the URL string to store into a dbase?

or could i track a cookie that the user gets?

(very layman's terms here, sorry)

if http referrer is domain.com add to url '&referer=google'

which should stay with them during their session.

OR

when a user clicks my Google adwords ad. they get a cookie with a referring domain in it. try to read that cookie, and append the same variable.

any thoughts?

+1  A: 

Yes, you can do this.

How best to track depends on what you're trying to do with the information. If you need to know the referrer for the current visit on every pageview in that visit, then setting a cookie on the first (aka referred) pageview in that visit is a good idea. That way every page can have access to the referral domain.

If you only need the info for logging purposes (e.g. to double-check billing info from a PPC ad vendor), then you don't need to store in a cookie, simply detect that you have a referrer and log that fact. It's probably easier to do this in server code rather than client script.

You didn't note which server platform you were running on, so it's hard to give specific code samples or advice, but in javascript, here's a quick code sample I had from a while ago, which creates a tracking image (aka "web bug") to record a pageview, including referral information in an optional "r=" query string parameter. Adapting this to do what you want to do shouldn't be hard.

<script type="text/javascript">
document.write("<img src=\"http://yoursite.com/logviews/?TYPE=PV" 
    + ((document.referrer == "" || document.referrer == null) ? "" : "&amp;r=" + encodeURIComponent(document.referrer)) 
    + "\" width=\"0\" height=\"0\" alt=\"Page view tracker\" />");
</script>
Justin Grant
W2k3, IIS 6. that is great. thanks
tony noriega
Definitely do this stuff server-side.
Bob Aman