Knowledge Base/Integrations and advanced use

Record Visitors Based on Referral Source

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

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

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 common use cases:

Record Visits from a Single Referral Source

<script type="text/javascript"> 
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/123.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 tracking code would only be included for visitors from http://example.com

Never Record Visits from a Single Referral Source
 

<script type="text/javascript"> 
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/123.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://example.com.

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

<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/123.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, multiple TLDs (e.g. .co.in, .co.it, etc.) and paths within the site (e.g. http://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, including 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.