./ct_report/coverage/mongoose_system_metrics_sender.COVER.html

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 33 Reports = build_reports_for_each_tracking_id(ClientId, TrackingIds, ReportStructs),
16 33 send_reports(Reports),
17 33 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 33 lists:map(
24 fun(Tid) ->
25 35 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 35 lists:map(
33 fun(Report) ->
34 3887 build_report(ClientId, TrackingId, Report)
35 end, ReportStructs).
36
37 send_reports(ReportsList) ->
38 33 Url = get_url(),
39 33 lists:map(
40 fun(Reports) ->
41 35 flush_reports(Url, Reports)
42 end, ReportsList).
43
44 get_url() ->
45 33 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 213 Headers = [],
54 213 ContentType = "",
55 213 Body = string:join(Lines, "\n"),
56 213 Request = {ReportUrl, Headers, ContentType, Body},
57 213 httpc:request(post, Request, [], []);
58 flush_reports(ReportUrl, Lines) ->
59 178 {NewBatch, RemainingLines} = lists:split(20, Lines),
60 178 flush_reports(ReportUrl, NewBatch),
61 178 flush_reports(ReportUrl, RemainingLines).
62
63 build_report(ClientId, TrackingId, #{report_name := EventCategory, key := EventAction, value := EventLabel}) ->
64 3887 LstClientId = term_to_string(ClientId),
65 3887 LstEventCategory = term_to_string(EventCategory),
66 3887 LstEventAction = term_to_string(EventAction),
67 3887 LstEventLabel = term_to_string(EventLabel),
68 3887 LstLine = [
69 "v=1",
70 "&tid=", TrackingId,
71 "&t=event",
72 "&cid=", LstClientId,
73 "&ec=", LstEventCategory,
74 "&ea=", LstEventAction,
75 "&el=", LstEventLabel],
76 3887 string:join(LstLine, "").
77
78 term_to_string(Term) when is_binary(Term) ->
79 70 term_to_string(binary_to_list(Term));
80 term_to_string(Term) ->
81 15548 R = io_lib:format("~p",[Term]),
82 15548 lists:flatten(R).
Line Hits Source