Knowledge Base/Integrations and advanced use/Advanced use

Record Visitors Based on Referral Source

Mouseflow
posted this on June 28, 2013, 17:59

With Mouseflow, you can determine whether to record visitors based on the referral source. This requires the use of regular expressions and JavaScript to conditionally include the tracking code and start the recording. 

The examples below rely on the jQuery Cookie Plugin - you’ll need to download and install it.

Depending on which scenarios you want to record/not record, you have a few options. Here are some of the common use cases:

Record Visits from a Single Referral Source

<!-- Replace your existing tracking code with something that follows this format -->
<script type="text/javascript">
// URL of referral source
var ReferrerToRecord = "http://example.com"; (function($) {
var activateMouseflow = function() {
var mf = document.createElement("script"); mf.type = "text/javascript"; mf.async = true;
mf.src = "//cdn.mouseflow.com/projects/your-website-id.js";
document.getElementsByTagName("head")[0].appendChild(mf);
};
var mfReferrerCookie = $.cookie('mf_referrer');
if (mfReferrerCookie === null) {
$.cookie('mf_referrer', document.referrer);
mfReferrerCookie = document.referrer;
}
if (mfReferrerCookie == ReferrerToRecord) {
activateMouseflow();
}})(jQuery); </script>

 

In this case, the recording would only start for visitors from http://example.com.

Never Record Visits from a Single Referral Source

<!-- Replace your existing tracking code with something that follows this format -->
<script type="text/javascript">
// URL of referral source to ignore
var ReferrerToIgnore = "http://example.com";
(function($) {
var activateMouseflow = function() {
var mf = document.createElement("script"); mf.type = "text/javascript"; mf.async = true;
mf.src = "//cdn.mouseflow.com/projects/your-website-id.js";
document.getElementsByTagName("head")[0].appendChild(mf);
};
var mfReferrerCookie = $.cookie('mf_referrer');
if (mfReferrerCookie === null) {
$.cookie('mf_referrer', document.referrer);
mfReferrerCookie = document.referrer;
}

if (mfReferrerCookie != ReferrerToIgnore) {
activateMouseflow();
}
})(jQuery);
</script>


In this case, the recording will start for all visitors save the ones from http://example.com.

Never Record Visits From Multiple Referral Sources (Based on Pattern)

<!-- Replace your existing tracking code with something that follows this format -->
<script type="text/javascript">
var ReferrerToIgnore = "/^https?:\/\/([^\/]+\.)?google\.co\.in(\/|$)/i)";
(function($) {
var activateMouseflow = function() {
var mf = document.createElement("script"); mf.type = "text/javascript"; mf.async = true;
mf.src = "//cdn.mouseflow.com/projects/your-website-id.js";
document.getElementsByTagName("head")[0].appendChild(mf);
};
var mfReferrerCookie = $.cookie('mf_referrer');
if (mfReferrerCookie === null) {
$.cookie('mf_referrer', document.referrer);
mfReferrerCookie = document.referrer;
}

if (mfReferrerCookie != ReferrerToIgnore) {
activateMouseflow();
}
})(jQuery);
</script>

 

In this case, the tracking code would not be included for visitors from http://google.co.* or https://google.co.*. Since it uses a matching pattern and multiple TLDs (e.g. .co.in, .co.it, etc.), the paths within the site (e.g. https://google.co.in/abc/123/abc) are supported.

Notes

  • The URL on the "mf.src" line must be customized to match the tracking code in your account
  • You can test patterns, include the example above with Rubular
  • You should always test these examples to make sure they have the desired functionality

Questions?

If you have questions or need help, feel free to open a ticket or call us at +1 (855) 668-7335.

Tags: record visitors referral source jquery plugin