Track user activity


#1

Hello!

Firts of all I have to say that I am just learning piwik and I am not JavaScript programmer. But I have a small feature proposal.

I have a site, where users usialy acces only one page and read it for a long time. I know, that some of them keep it open for days.

This make me think about way to track they activities. I have added two pieсes of code to piwik.js. They are not perfect, but it works for me.

The code tracks the user activity and sends messages to server when:

  • user is not active for “timeout” seconds;
  • user resumes the activity;
  • user was active for “min” seconds and closing the window.

May be somebody improve the idea.

The first piece (this function is called when user resumes activity):


          function logActivity(customTitle, customData) {
                var  request = getRequest('new_visit=1&action_name=' + encodeWrapper(titleFixup(customTitle || configTitle)), customData, 'log');

                sendRequest(request, configTrackerPause);
            }

And the second:


               trackActivity: function (min, timeout, beacon ) {

                        if ( !isDefined(min) || !isDefined(timeout) || min <= 0 || timeout <=0 ) return;

                        var isActive = true;
                        var theBeginning = new Date();

                        function logInactivity() {
                                isActive = false;
                                timeoutId = null;
                                logEvent('activity','inactive');
                        }

                        var timeoutId = window.setTimeout(logInactivity, timeout * 1000);

                        if ( isDefined(beacon) && beacon > 0 ) {
                                window.setInterval( function ()
                                                        {
                                                                if(isActive) {
                                                                        logEvent('activity','beacon');
                                                                }
                                                        }, beacon * 1000);
                        }

                        window.onbeforeunload = window.onunload = function ()
                                                        {
                                                                //I should check that this is not a transfer to another page of the same site. How?
                                                                var timeNow = new Date();
                                                                if (isActive && (timeNow - theBeginning) > min * 1000 ) {
                                                                        logEvent('activity','closing');
                                                                }
                                                                window.onbeforeunload = window.onunload = null;
                                                        };

                        function activityHandler() {
                                if (!isActive) {
                                        isActive = true;
                                        theBeginning = new Date();
                                        logActivity();

                                }
                                if ( timeoutId !== null ) {
                                        window.clearTimeout(timeoutId);
                                }
                                timeoutId = window.setTimeout(logInactivity, timeout * 1000);
                        }


                        //I have copied this from logPageView
                        //--------------------------------------------------------------------------
                        // add event handlers; cross-browser compatibility here varies significantly
                        // @see http://quirksmode.org/dom/events
                        addEventListener(documentAlias, 'click', activityHandler);
                        addEventListener(documentAlias, 'mouseup', activityHandler);
                        addEventListener(documentAlias, 'mousedown', activityHandler);
                        addEventListener(documentAlias, 'mousemove', activityHandler);
                        addEventListener(documentAlias, 'mousewheel', activityHandler);
                        addEventListener(windowAlias, 'DOMMouseScroll', activityHandler);
                        addEventListener(windowAlias, 'scroll', activityHandler);
                        addEventListener(documentAlias, 'keypress', activityHandler);
                        addEventListener(documentAlias, 'keydown', activityHandler);
                        addEventListener(documentAlias, 'keyup', activityHandler);
                        addEventListener(windowAlias, 'resize', activityHandler);
                        addEventListener(windowAlias, 'focus', activityHandler);
                        addEventListener(windowAlias, 'blur', activityHandler);
                       //---------------------------------------------------------------------------                   

                }



(Matthieu Aubry) #2

Nice suggestion. Btw here is the official request to improve Piwik to track accurate time on page & activity: Support Ping requests to report a better 'time on page' · Issue #2041 · matomo-org/matomo · GitHub