Determine if a user came is an organic or direct visit?

6.4k views Asked by At

Is it possible to determine whether a user has come to your website via an organic search or direct and is it then possible to store this in our database?

We have a form on our website so essentially we want to find out if the user came direct or via organic search and then pass this in with the form to store 'organic' or 'direct' in our database for each enquiry.

E.g if user ABC came via direct and filled in the form they would be stored in the database as name = ABC, referral = direct, enquiry = test.

If user XYZ came via organic and filled in the form they would be stored in the database as name = XYZ, referral = organic, enquiry = test.

3

There are 3 answers

3
MMMdata On

As I understand it, you are trying to pass a value stored within the GA cookie to your CRM database. If so, there are a number of ways you could accomplish what you are looking for.

Like others have said, I would recommend parsing the data from the cookie. There's also the _link() or for you, the _linkByPost() function which will append the cookie information as URL parameters. Perhaps then you could take the values from the URL parameters: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._linkByPost

In this case, you would have a script on the form confirmation URL that would listen to the URL parameters than contain referrer information, and pass that along to your database. Here is an example of what that URL would look like:

www.example.com?__utma=224048396.1415920582.1378332727.1378332727.1378332727.1&__utmb=224048396.2.10.1378332727&__utmc=224048396&__utmx=-&__utmz=224048396.1378332727.1.1.utmcsr=stackoverflow.com|utmccn=(referral)|utmcmd=referral|utmcct=/forum/&__utmv=-&__utmk=125169161

Have you considered using the API? That might be another option. Check out this API tool, it's quite helpful: http://ga-dev-tools.appspot.com/explorer/.

Can you provide a snippet of the code that's storing the values associated with a given user?

4
Nicholas Post On

You could try to request the browsers "referrer" parameter. However, this method is extremely unreliable and is often unavailable. The best option would be to have a section (maybe even make it required) where the user selects where they come from.

The reason is what if they found your site on Google at work, then went home and manually typed in the URL? You would count it as a direct visit even though it was really a search engine. That's why most forms typically have a dropdown for this selection.

You could always grab both the referrer as well as their choice and use those for reporting.

Update: Since you mentioned you are tracking a PPC campaign, I have a few methods that you can use to better track the source.

The first method is to create a landing page for your ad to click over to. Basically, the page will be almost identical to the form on your website, but will instead be a standalone page or subdomain. This page will have a duplicate form that will have the referral value hard coded in. You could have a unique page/site for each campaign source (yoursite/google.aspx, yoursite.com/yahoo.aspx, etc.)

A second method would be to use a single form to take in the submission, but in your campaign assign a querystring variable that will represent the campaign (yourpage.aspx?r=google, yourpage.aspx?r=yahoo, etc.) Once the user loads the page, grab that querystring variable and save it in their session or in a cookie. That way if they browse your site and go back to the form, you would still have their referrer.

As always, these methods don't work if they click to your site from an ad and then manually type it in at home. This isn't super common but it does happen occasionally.

4
cmorrissey On

You can use $_SERVER['HTTP_REFERER']

PHP DOCS: The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

http://php.net/manual/en/reserved.variables.server.php