1 |
|
-module(mongoose_system_metrics_sender). |
2 |
|
|
3 |
|
-define(BASE_URL, "https://www.google-analytics.com/batch"). |
4 |
|
|
5 |
|
-export([send/3]). |
6 |
|
|
7 |
|
-type google_analytics_report() :: string(). |
8 |
|
-type url() :: string(). |
9 |
|
-type report_struct() :: mongoose_system_metrics_collector:report_struct(). |
10 |
|
|
11 |
|
-spec send(service_mongoose_system_metrics:client_id(), |
12 |
|
[report_struct()], |
13 |
|
[service_mongoose_system_metrics:tracking_id()]) -> ok. |
14 |
|
send(ClientId, ReportStructs, TrackingIds) -> |
15 |
40 |
Reports = build_reports_for_each_tracking_id(ClientId, TrackingIds, ReportStructs), |
16 |
40 |
send_reports(Reports), |
17 |
40 |
ok. |
18 |
|
|
19 |
|
-spec build_reports_for_each_tracking_id(service_mongoose_system_metrics:client_id(), |
20 |
|
[service_mongoose_system_metrics:tracking_id()], |
21 |
|
[report_struct()]) -> [google_analytics_report()]. |
22 |
|
build_reports_for_each_tracking_id(ClientId, TrackingIds, ReportStructs) -> |
23 |
40 |
lists:map( |
24 |
|
fun(Tid) -> |
25 |
42 |
build_reports(ClientId, Tid, ReportStructs) |
26 |
|
end, TrackingIds). |
27 |
|
|
28 |
|
-spec build_reports(service_mongoose_system_metrics:client_id(), |
29 |
|
service_mongoose_system_metrics:tracking_id(), |
30 |
|
[report_struct()]) -> [google_analytics_report()]. |
31 |
|
build_reports(ClientId, TrackingId, ReportStructs) -> |
32 |
42 |
lists:map( |
33 |
|
fun(Report) -> |
34 |
4461 |
build_report(ClientId, TrackingId, Report) |
35 |
|
end, ReportStructs). |
36 |
|
|
37 |
|
send_reports(ReportsList) -> |
38 |
40 |
Url = get_url(), |
39 |
40 |
lists:map( |
40 |
|
fun(Reports) -> |
41 |
42 |
flush_reports(Url, Reports) |
42 |
|
end, ReportsList). |
43 |
|
|
44 |
|
get_url() -> |
45 |
40 |
mongoose_config:get_opt(google_analytics_url, ?BASE_URL). |
46 |
|
|
47 |
|
% % https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch-limitations |
48 |
|
% % A maximum of 20 hits can be specified per request. |
49 |
|
-spec flush_reports(url(), [google_analytics_report()]) -> {ok, term()} | {error, term()}. |
50 |
|
flush_reports(_, []) -> |
51 |
:-( |
{ok, nothing_to_do}; |
52 |
|
flush_reports(ReportUrl, Lines) when length(Lines) =< 20 -> |
53 |
243 |
Headers = [], |
54 |
243 |
ContentType = "", |
55 |
243 |
Body = string:join(Lines, "\n"), |
56 |
243 |
Request = {ReportUrl, Headers, ContentType, Body}, |
57 |
243 |
httpc:request(post, Request, [], []); |
58 |
|
flush_reports(ReportUrl, Lines) -> |
59 |
201 |
{NewBatch, RemainingLines} = lists:split(20, Lines), |
60 |
201 |
flush_reports(ReportUrl, NewBatch), |
61 |
201 |
flush_reports(ReportUrl, RemainingLines). |
62 |
|
|
63 |
|
build_report(ClientId, TrackingId, #{report_name := EventCategory, key := EventAction, value := EventLabel}) -> |
64 |
4461 |
LstClientId = term_to_string(ClientId), |
65 |
4461 |
LstEventCategory = term_to_string(EventCategory), |
66 |
4461 |
LstEventAction = term_to_string(EventAction), |
67 |
4461 |
LstEventLabel = term_to_string(EventLabel), |
68 |
4461 |
LstLine = [ |
69 |
|
"v=1", |
70 |
|
"&tid=", TrackingId, |
71 |
|
"&t=event", |
72 |
|
"&cid=", LstClientId, |
73 |
|
"&ec=", LstEventCategory, |
74 |
|
"&ea=", LstEventAction, |
75 |
|
"&el=", LstEventLabel], |
76 |
4461 |
string:join(LstLine, ""). |
77 |
|
|
78 |
|
term_to_string(Term) when is_binary(Term) -> |
79 |
84 |
term_to_string(binary_to_list(Term)); |
80 |
|
term_to_string(Term) -> |
81 |
17844 |
R = io_lib:format("~p",[Term]), |
82 |
17844 |
lists:flatten(R). |