Sha256: f47b3f91f7f8d9702cbcb2ba19f4ed340e70a819653ca9f6dcd42d4b595d6651

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require "faraday"
require "typhoeus"
require "typhoeus/adapters/faraday"

require "garsh/version"

class Garsh
  attr_reader :code
  GA_ENDPOINT = "https://www.google-analytics.com"

  def self.client(code = ENV['GA_CODE'])
    return new(code)
  end

  def initialize(code)
    @code = code
  end

  def create_event(action:, category: "", client_id: "555")
    event = Event.new(code: code, action: action, category: category, client_id: client_id)
    send_payload(Event::ENDPOINT, event.to_params)
  end

  def send_payload(endpoint, params)
    @http_client ||= Faraday.new(url: GA_ENDPOINT) { |cli|
      cli.adapter :typhoeus
      cli.response :logger
    }
    @http_client.get(endpoint, params)
  end

  class Event
    attr_reader   :version
    attr_reader   :tracking_code
    attr_accessor :client_id
    attr_accessor :category
    attr_accessor :action

    ENDPOINT = "collect"

    EVENT_ID = "event"

    def initialize(opts={})
      # required
      @tracking_code = opts.fetch(:code)
      @action = opts.fetch(:action)
      # optional
      @client_id = opts.fetch(:client_id, "")
      @version = opts.fetch(:version, 1)
      @category = opts.fetch(:category, "")
    end

    def to_params
      {
        v: version,
        tid: tracking_code,
        cid: client_id,
        t: EVENT_ID,
        ec: category,
        ea: action
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
garsh-0.1.0 lib/garsh.rb