📜 ⬆️ ⬇️

Calculation of statistics of downloads in Google Analytics


Continuing the topic of using the bundle Nginx + Lua decided to share a small instruction on how to calculate the load of static data and taking all this into account in Google Analytics.

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

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

Simple track record

The easiest way, which 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 admire run records in GA.
')
Accounting through events

This method requires you to determine how we will qualify the downloaded files. In GA terms, we need to distinguish 3 entities: category, action, label. In his case, I used folder names for this, then the final URL of the uploaded file was: 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 reboot. Now we can keep statistics on downloads in a convenient form, make various cuts.

Performance

We will check how quickly our counter will work, 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 cap is taken from here .

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


All Articles