Analyze download statistics

  • Tutorial

Continuing the topic of using the Nginx + Lua bundle, I decided to share a little instruction on counting the loading of static data and taking all of this into account in Google Analytics.

We assume that the nginx-lua bundle is already configured and we have a counter name. If not, then the network has a lot of instructions, there are also ready-made assemblies ( openresty.org ).
For accounting, we need a script that will send data to GA. I didn’t want to write my own, and an example was found on Github’s open spaces: github.com/wstucco/ssga.lua This script has one track method that takes the name of the counter as a required parameter, the host name and the path that must be passed can be passed . After it is triggered, in the real-time statistics panel, we will see our transmitted path.
There are two ways to consider downloads:
  • take into account the path of the downloaded file;
  • account for through events.

The first option is convenient when there is no sense in dividing files into categories / groups, or when it is not possible to do this, but you want to keep statistics. The second option is suitable for cases where there is a strict file structure, for example: section-category-type.
Consider both.

Simple way tracking

The easiest way that does not require any special manipulation and preparation. Therefore, I will give only the final part of the nginx config.

resolver  8.8.8.8;
access_by_lua '    
     local ssga = require( "ssga" )
     ssga.track({ua = "UA-25XXXXXX-1")
';

Reboot and enjoy the running records in GA.

Event Accounting

This method requires determining how we will qualify the downloaded files. In terms of GA, we need to distinguish 3 entities: category, action, label. In my case, I used folder names for this, then the final URL of the downloaded file looked like: dl.domain.name/category/action/label/filename.ext
It was also necessary to teach the existing script to transfer this information to GA. After minor modifications, an extended version of the script was obtained ( github.com/fuCtor/ssga.lua ).
As a result, we have the following configuration:

server {
  listen 80;
  server_name dl.domain.name;
  root /var/www/public;
  error_page 502 /502.html;
  location / {
    add_header Cache-Control public;
    expires  max;
    resolver 8.8.8.8;
    access_by_lua '
        opt = {}
        index  = 0
        for value in string.gmatch(ngx.var.uri ,"%w+") do
         opt [index] = value
         index = index + 1
        end
        local ssga = require( "ssga" )
        ssga.event({ua = "UA-25XXXXXX-1", domain = "dl.domain.name", category = opt[0], action = opt[1], label = opt[2], value = 1})
    ';
    }
}

Save reload. Now we can keep statistics on downloads in a convenient form, make various slices.

Performance

We will check how exactly our counter will work out quickly, so we will execute HEAD requests, the body does not interest us.

Concurrency Level:      30
Time taken for tests:   3.223 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      348000 bytes
HTML transferred:       0 bytes
Requests per second:    310.26 [#/sec] (mean)
Time per request:       96.694 [ms] (mean)
Time per request:       3.223 [ms] (mean, across all concurrent requests)
Transfer rate:          105.44 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    94   95   0.6     95      99
Waiting:       94   95   0.6     95      99
Total:         94   95   0.6     95      99

Concurrency Level:      30
Time taken for tests:   0.048 seconds
Complete requests:      200
Failed requests:        0
Write errors:           0
Total transferred:      69600 bytes
HTML transferred:       0 bytes
Requests per second:    4166.58 [#/sec] (mean)
Time per request:       7.200 [ms] (mean)
Time per request:       0.240 [ms] (mean, across all concurrent requests)
Transfer rate:          1415.99 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:     6    6   0.8      6       9
Waiting:        6    6   0.8      6       9
Total:          6    7   0.9      6      10


The image in the header is taken from here .

Also popular now: