Class: TrophyApiClient::AsyncMetricsClient

Inherits:
Object
  • Object
show all
Defined in:
lib/trophy_api_client/metrics/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_client:) ⇒ TrophyApiClient::AsyncMetricsClient

Parameters:



80
81
82
# File 'lib/trophy_api_client/metrics/client.rb', line 80

def initialize(request_client:)
  @request_client = request_client
end

Instance Attribute Details

#request_clientTrophyApiClient::AsyncRequestClient (readonly)



76
77
78
# File 'lib/trophy_api_client/metrics/client.rb', line 76

def request_client
  @request_client
end

Instance Method Details

#event(key:, user:, value:, idempotency_key: nil, attributes: nil, request_options: nil) ⇒ TrophyApiClient::EventResponse

Increment or decrement the value of a metric for a user.

Examples:

api = TrophyApiClient::Client.new(
  base_url: "https://api.example.com",
  environment: TrophyApiClient::Environment::DEFAULT,
  api_key: "YOUR_API_KEY"
)
api.metrics.event(
  idempotency_key: "e4296e4b-8493-4bd1-9c30-5a1a9ac4d78f",
  key: "words-written",
  user: { email: "[email protected]", tz: "Europe/London", attributes: { "department": "engineering", "role": "developer" }, id: "18" },
  value: 750,
  attributes: { "category": "writing", "source": "mobile-app" }
)

Parameters:

  • idempotency_key (String) (defaults to: nil)
  • key (String)

    Unique reference of the metric as set when created.

  • user (Hash)

    The user that triggered the event.Request of type TrophyApiClient::UpsertedUser, as a Hash

    • :id (String)

    • :email (String)

    • :name (String)

    • :tz (String)

    • :device_tokens (Array<String>)

    • :subscribe_to_emails (Boolean)

    • :attributes (Hash=> String)

  • value (Float)

    The value to add to the user’s current total for the given metric.

  • attributes (Hash{String => String}) (defaults to: nil)

    Event attributes as key-value pairs. Keys must match existing event attributes set up in the Trophy dashboard.

  • request_options (TrophyApiClient::RequestOptions) (defaults to: nil)

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/trophy_api_client/metrics/client.rb', line 114

def event(key:, user:, value:, idempotency_key: nil, attributes: nil, request_options: nil)
  Async do
    response = @request_client.conn.post do |req|
      req.options.timeout = request_options.timeout_in_seconds unless request_options&.timeout_in_seconds.nil?
      req.headers["X-API-KEY"] = request_options.api_key unless request_options&.api_key.nil?
      req.headers = {
        **(req.headers || {}),
        **@request_client.get_headers,
        **(request_options&.additional_headers || {}),
        "Idempotency-Key": idempotency_key
      }.compact
      unless request_options.nil? || request_options&.additional_query_parameters.nil?
        req.params = { **(request_options&.additional_query_parameters || {}) }.compact
      end
      req.body = {
        **(request_options&.additional_body_parameters || {}),
        user: user,
        value: value,
        attributes: attributes
      }.compact
      req.url "#{@request_client.get_url(request_options: request_options)}/metrics/#{key}/event"
    end
    TrophyApiClient::EventResponse.from_json(json_object: response.body)
  end
end