Class: Klaviyo::Metrics

Inherits:
Client
  • Object
show all
Defined in:
lib/klaviyo/apis/metrics.rb

Constant Summary collapse

EXPORT =
'export'

Constants inherited from Client

Client::ALL, Client::BASE_API_URL, Client::CONTENT_JSON, Client::CONTENT_URL_FORM, Client::DEFAULT_COUNT, Client::DEFAULT_PAGE, Client::DEFAULT_SORT_DESC, Client::HTTP_DELETE, Client::HTTP_GET, Client::HTTP_POST, Client::HTTP_PUT, Client::METRIC, Client::METRICS, Client::TIMELINE, Client::V1_API, Client::V2_API

Class Method Summary collapse

Class Method Details

.get_metric_export(metric_id, start_date: nil, end_date: nil, unit: nil, measurement: nil, where: nil, by: nil, count: nil) ⇒ Object

Export event data, optionally filtering and segmented on available event properties

Parameters:

  • metric_id (String)

    the id of the metric

  • start_date (String) (defaults to: nil)

    Beginning of the timeframe to pull event data for. Default is 1 month ago

  • end_date (String) (defaults to: nil)

    End of the timeframe to pull event data for. Default is the current day

  • unit (String) (defaults to: nil)

    Granularity to bucket data points into - one of ‘day’, ‘week’, or ‘month’. Defaults to ‘day’.

  • measurement (String or JSON-encoded list) (defaults to: nil)

    Type of metric to fetch

  • where (JSON-encoded list) (defaults to: nil)

    Conditions to use to filter the set of events. A max of 1 condition can be given.

  • by (String) (defaults to: nil)

    The name of a property to segment the event data on. Where and by parameters cannot be specified at the same time.

  • count (Integer) (defaults to: nil)

    Maximum number of segments to return. The default value is 25.

Returns:

  • A dictionary relecting the input request parameters as well as a results property



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/klaviyo/apis/metrics.rb', line 58

def self.get_metric_export(metric_id,
                           start_date: nil,
                           end_date: nil,
                           unit: nil,
                           measurement: nil,
                           where: nil,
                           by: nil,
                           count: nil
                           )
  path = "#{METRIC}/#{metric_id}/#{EXPORT}"
  params = {
    :start_date => start_date,
    :end_date => end_date,
    :unit => unit,
    :measurement => measurement,
    :where => where,
    :by => by,
    :count => count
  }
  v1_request(HTTP_GET, path, params)
end

.get_metric_timeline(metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC) ⇒ Object

Returns a batched timeline for one specific type of metric.

Parameters:

  • metric_id (String)

    the id of the metric

  • since (Integer or String) (defaults to: nil)

    either a Unix timestamp or the UUID from a previous request. Default is the current time.

  • count (Integer) (defaults to: DEFAULT_COUNT)

    number of results to return, default 100

  • sort (String) (defaults to: DEFAULT_SORT_DESC)

    ‘asc’ or ‘desc’, sort order to apply to the timeline. Default is ‘desc’.

Returns:

  • a dictionary with a data property that contains information about what metric the event tracks



38
39
40
41
42
43
44
45
46
# File 'lib/klaviyo/apis/metrics.rb', line 38

def self.get_metric_timeline(metric_id, since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
  path = "#{METRIC}/#{metric_id}/#{TIMELINE}"
  params = {
    :since => since,
    :count => count,
    :sort => sort
  }
  v1_request(HTTP_GET, path, params)
end

.get_metrics(page: DEFAULT_PAGE, count: DEFAULT_COUNT) ⇒ Object

Returns a list of all metrics in Klaviyo

Parameters:

  • page (Integer) (defaults to: DEFAULT_PAGE)

    which page to return, default 0

  • count (Integer) (defaults to: DEFAULT_COUNT)

    number of results to return, default 100

Returns:

  • a dictionary with a data property that contains an array of all the metrics



9
10
11
12
13
14
15
# File 'lib/klaviyo/apis/metrics.rb', line 9

def self.get_metrics(page: DEFAULT_PAGE, count: DEFAULT_COUNT)
  params = {
    :page => page,
    :count => count
  }
  v1_request(HTTP_GET, METRICS, params)
end

.get_metrics_timeline(since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC) ⇒ Object

Returns a batched timeline of all events in your Klaviyo account.

Parameters:

  • since (Integer or String) (defaults to: nil)

    either a Unix timestamp or the UUID from a previous request. Default is the current time.

  • count (Integer) (defaults to: DEFAULT_COUNT)

    number of results to return, default 100

  • sort (String) (defaults to: DEFAULT_SORT_DESC)

    ‘asc’ or ‘desc’, sort order to apply to the timeline. Default is ‘desc’.

Returns:

  • a dictionary with a data property that contains an array of the metrics



22
23
24
25
26
27
28
29
30
# File 'lib/klaviyo/apis/metrics.rb', line 22

def self.get_metrics_timeline(since: nil, count: DEFAULT_COUNT, sort: DEFAULT_SORT_DESC)
  path = "#{METRICS}/#{TIMELINE}"
  params = {
    :since => since,
    :count => count,
    :sort => sort
  }
  v1_request(HTTP_GET, path, params)
end