Knowledge base | mouseflow

Start Recordings Based on User Actions

With Mouseflow, you can choose to only record visitors who perform a specific action - like logging in, adding an item to the cart, or simply clicking a button.

This requires the use of cookies and JavaScript to conditionally include the tracking code. The examples below rely on the js-cookie library. Add this line to use it on your site:

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

In the example below, we are starting recordings when the HTML element with the ID example is clicked. This condition can of course be changed to whatever suits you.

Example:

<script type="text/javascript">
    window._mfq = window._mfq || [];
    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); 
    };

    // Action to trigger Mouseflow here:
    $("#example").click(function() {
        Cookies.set('mf_start', '1');
        activateMouseflow();
    });

    if (Cookies.get('mf_start') === '1') {
        activateMouseflow();
    }
</script>

Notes

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