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 js-cookie library - you'll need to add it to your page with this line:

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.4/js.cookie.min.js"></script>


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 activateMouseflow() { 
        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 = Cookies.get('mf_referrer'); 
    if (mfReferrerCookie === null) {
        Cookies.set('mf_referrer', document.referrer);
        mfReferrerCookie = document.referrer; 
    }

    if (mfReferrerCookie === referrerToRecord) {
        activateMouseflow();
    }
</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 regex101.com
  • You should always test these examples to make sure they have the desired functionality