Last active
April 24, 2016 19:34
-
-
Save jordangraft/30aea30264259b1145b0e37990318278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/workers/analytics_worker.rb | |
## | |
# A Sidekiq worker that handles sending order data to Analytics | |
# Measurement Protocol | |
class AnalyticsWorker | |
include Sidekiq::Worker | |
sidekiq_options retry: 2 | |
def perform(id) | |
Order.find(id).send_to_ga | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LineItem < ActiveRecord::Base | |
belongs_to :order | |
## | |
# Sending subscription to GA https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom | |
# v=1 // Version. | |
# &tid=UA-XXXXX-Y // Tracking ID / Property ID. | |
# &cid=555 // Anonymous Client ID. | |
# &t=item // Item hit type. | |
# &ti=12345 // Transaction ID. Required. | |
# &in=sofa // Item name. Required. | |
# &ip=300 // Item price. | |
# &iq=2 // Item quantity. | |
# &ic=u3eqds43 // Item code / SKU. | |
# &iv=furniture // Item variation / category. | |
def send_to_ga | |
url = "https://www.google-analytics.com/collect?v=1&t=item&tid=#{GA_ID}&cid=#{order.ga_id}&ti=#{order_id}&in=#{ga_name}&ip=#{revenue}&iq=1&ic=#{provider.try(:id)}&iv=#{service_type.try(:name)}" | |
RestClient.post(url, {}) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
after_commit :queue_ga_worker | |
has_many :line_items | |
## | |
# Queue sidekiq worker to perform task of sending orders and subscriptions | |
# to Google Analytics | |
def queue_ga_worker | |
AnalyticsWorker.perform_async(id) | |
end | |
## | |
# Send order to GA Measurement Protocol | |
# https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom | |
def send_to_ga | |
return nil unless ga_id | |
return nil unless completed? | |
ga_transaction_req | |
line_items.each(&:send_to_ga) | |
return true | |
end | |
## | |
# https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom | |
# v=1 // Version. | |
# &tid=UA-XXXXX-Y // Tracking ID / Property ID. | |
# &cid=555 // Anonymous Client ID. | |
# &t=transaction // Transaction hit type. | |
# &ti=12345 // transaction ID. Required. | |
# &ta=westernWear // Transaction affiliation. | |
# &tr=50.00 // Transaction revenue. | |
# &ts=32.00 // Transaction shipping. | |
# &tt=12.00 // Transaction tax. | |
# &cu=EUR // Currency code. | |
def ga_transaction_req | |
url = "https://www.google-analytics.com/collect?v=1&t=transaction&tid=#{GA_ID}&cid=#{ga_id}&ti=#{id}&tr=#{revenue}" | |
RestClient.post(url, {}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment