Class: IshManager::AnalyticsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/analytics_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home, #tinymce

Instance Method Details

#indexObject



6
7
8
# File 'app/controllers/ish_manager/analytics_controller.rb', line 6

def index
  authorize! :analytics, IshManager::Ability
end

#testObject



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/ish_manager/analytics_controller.rb', line 10

def test
  authorize! :analytics, IshManager::Ability

  if params[:prev_interval].present?
    if !@prev_intervals.include?( params[:prev_interval] )
      flash_alert "prev_interval not allowed"
      redirect_to action: :index
      return
    end
    prev_interval = eval( params[:prev_interval] )
    selected_date = "#{(Time.now - prev_interval ).to_date.to_s},#{Time.now.to_date.to_s}"
  else
    selected_date = "#{params[:date_from]},#{params[:date_to]}"
  end

  base_url = "https://analytics.wasya.co/index.php?force_api_session=1&module=API&format=JSON&"
  # api_url_trash = "&idDimension=1&idSite=2&period=day&date=2023-07-01,2023-09-01"
  opts = {
    "token_auth" => ANALYTICS_TOKEN,
    "filter_limit" => "-1",
    "expanded" => 1,

    # "method" => "Actions.getPageUrls",
    method: params[:method],

    "idDimension" => 1,

    # "idSite" => 2,
    idSite: params[:idSite],

    # "period" => "day",
    "period" => "range",
    # period: params[:period],

    "date" => selected_date,
  }

  cids    = []
  reports = {}
  reports[9999] = []

  puts! "#{base_url}#{opts.to_query}", 'ze query'

  inns = HTTParty.get( "#{base_url}#{opts.to_query}" )
  inns = JSON.parse( inns.body )

  if "Actions.getPageUrls" == params[:method]
    inns.each do |date, items|
      items.each do |item|

        path = item['label']
        if path[0] != '/'
          path = "/#{path}"
        end

        cid = path[/.*cid=([\d]*)/,1]&.to_i
        if cid
          cids.push cid
          reports[cid] ||= []
          reports[cid].push "#{date} :: #{path}"
        end
      end
    end
  end

  if "Live.getLastVisitsDetails" == params[:method]
    inns.each do |items|
      date = items['serverDate']
      items['actionDetails'].each do |item|
        path = item['url']

        cid = path[/.*cid=([\d]*)/,1]&.to_i
        if cid
          cids.push cid
          reports[cid] ||= []
          reports[cid].push "#{date} :: #{path}"
        else
          reports[9999].push "#{date} :: #{path}"
        end
      end
    end
  end

  cids        = cids.uniq.compact
  leads_h     = Lead.find_to_h cids
  @reports = {}

  reports.each do |k, v|
    lead_label = "[#{k}] <#{leads_h[k]&.name} #{leads_h[k]&.email}>"
    @reports[lead_label] = v
  end

  @reports = @reports.sort

end