-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupersonic.rb
77 lines (63 loc) · 2.2 KB
/
supersonic.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class AdtekioAdnetworks::Cost::Supersonic
include AdtekioAdnetworks::CostImport
define_required_credentials do
[:login, :key]
end
def load_data(uri)
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth credentials.login, credentials.key
res = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
:verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
http.request(req)
end
JSON.parse(res.body, :symbolize_names => true)[:data]
end
def campaigns
uri = Addressable::URI.
parse("https://platform.supersonic.com/partners/advertiser/campaigns")
load_data(uri)
end
def stats(campaign_ids, date)
uri = Addressable::URI.
parse("https://platform.supersonic.com/partners/advertiser/campaigns/stats")
uri.query_values = {
:start_date => date.strftime("%Y-%m-%d"),
:end_date => date.strftime("%Y-%m-%d"),
:campaign_id => campaign_ids.join(',')
}
load_data(uri)
end
def mobile_campaign_costs(from, till)
campaign_hsh = campaigns.group_by do |campaign|
campaign[:campaign_id].to_i
end
from.upto(till).map do |date|
stats(campaign_hsh.keys, date).map do |campaign_stats|
campaign_id = campaign_stats[:campaign_id].to_i
campaign = campaign_hsh[campaign_id].first
platform = case campaign[:target_platform].first
when 'apple_itunes' then 'ios'
when 'google_play' then 'android'
else nil
end
next if campaign[:bundle_id].nil?
campaign_stats[:data].map do |stats|
country = stats[:country_code].downcase
{
:date => date,
:campaign => campaign[:campaign_name],
:adgroup => country,
:impressions => stats[:impressions].to_i,
:clicks => stats[:clicks].to_i,
:conversions => stats[:conversions].to_i,
:amount => stats[:expense].to_f / 100,
:target_country => country,
}
end
end.compact.flatten
end.flatten
end
def campaign_costs(from, till)
mobile_campaign_costs(from, till)
end
end