Real-time event tracking (Javascript statistics)

There are many real-time web statistics on the Internet (w3counter-histats-gostats). They are simply counted once when the page is requested. But I want to track javascript functions / events because my site is completely -ajax.So I want something like google analyttic event tracker ( http://code.google.com/intl/en/apis/analytics/docs/gaJS /gaJSApiEventTracking.html ), but in real time and very simple. It can be hosted or just a PHP script. Thank.

event examples

  • user created something
  • user deleted something.
  • ajax error happend
  • eror happend browser
  • user logged in

I found my answer by looking

+3
source share
6 answers

It is not so difficult to implement your own . You simply send a request to the server when an event occurs: tracker.gif?action=create&what=sth, tracker.gif?action=error&what=k_is_undefinedetc.

You will then analyze the server logs for statistics. (or you send your request directly to the database tracker.php?action=create&what=sth)

Since you are managing the site, it is now easy to execute these requests when a user logs in or the ajax request fails.

For error handling you can use window.onerror:

// send a request about an event to the server
function fireEvent(action, message, options) {
  var loggerUrl = "/tracker.gif", parameters;
  options = options || {};
  options.url = options.url || window.location.href;
  options.user_agent = navigator.userAgent;
  options.message = message;
  for (var i in options) {
    if (options.hasOwnProperty(i)) {
      parameters += "&" + i + "=" + encodeURIComponent(options[i]);
    }
  }    
  new Image().src = loggerUrl + parameters;
}

// log script errors
window.onerror = function(errorMessage, url, line) {   
  fireEvent("error", errorMessage, {
    url: url, 
    line: line
  });
  return true;
};

// example event on the page
fireEvent("ajaxError", "XY page failed to load");

(note: window.onerrornot available on safari)


UPDATE

PHP:

$i = 1;
$d = file_get_contents("log.txt");
$requests = explode("\n", $d);
foreach ($requests as $req) {
  $pos = strpos($req, "tracker.gif");
  if ($pos === false) continue;
  $start_pos = strpos($req, "?", $pos);
  $end_pos = strpos($req, " ", $start_pos); // can also be " HTTP"
  $length = $end_pos - $start_pos;
  $req = substr($req, $start_pos+1, $length);
  $exprs = explode("&", $req);
  echo $i . ".<br>"; // request number
  $i += 1;
  foreach ($exprs as $expr) {
    list($name, $value) = explode("=", $expr);
    echo $name . " =>" . $value . "<br>"; // key => value
  }
}
+4

jQuery, $.ajaxSuccess event

+3

Google Analytics _trackPageview(), !

: , , , , , Jason $.ajaxSuccess() .

0
0

, google " ". .

mixpanel chartbeat.

0

- , Piwik .

There is a good JavaScript client for tracking events, some documentation .

To track events, I use this regular function, if necessary:

_paq.push( [ 'category', 'action', 'name', 'value' ] );

You can then export all the data from the Piwik web views or directly query the database for information if you host Piwik on your server.

0
source

Source: https://habr.com/ru/post/1753798/


All Articles