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