class Snogmetrics::KissmetricsApi
Public Class Methods
Do not instantiate KissmetricsApi
yourself, instead mix in Snogmetrics
and use it's km method to get an instance of KissmetricsApi
.
# File lib/snogmetrics/kissmetrics_api.rb, line 5 def initialize(api_key, session, output_strategy) @session = session @api_key = api_key @output_strategy = output_strategy end
Public Instance Methods
The equivalent of the `KM.identify` method of the JavaScript API.
# File lib/snogmetrics/kissmetrics_api.rb, line 22 def identify(identity) unless @session[:km_identity] == identity queue.delete_if { |e| e.first == 'identify' } queue << ['identify', identity] @session[:km_identity] = identity end end
In most situations you want the js!
method instead of this one.
Returns the JavaScript needed to send the current state to KISSmetrics. This includes the a JavaScript tag that loads the API code (or a fake API that sends events to the console or a global array if the `output_strategy` parameter to initialize is :console_log or :array), as well as statements that push the current state onto the `_kmq` array.
You can pass the option `:reset` to reset the state, this makes sure that subsequent calls to js
will not send events that have already been sent.
# File lib/snogmetrics/kissmetrics_api.rb, line 63 def js(options = {}) options = { reset: false }.merge(options) buffer = queue.map { |item| %(_kmq.push(#{item.to_json});) } queue.clear if options[:reset] <<-JAVASCRIPT <script type="text/javascript"> var _kmq = _kmq || []; #{api_js} #{buffer.join("\n")} </script> JAVASCRIPT end
Equivalent to `js(:reset => true)`, i.e. returns the JavaScript code needed to load the KISSmetrics API and send the current state, and reset the state afterwards.
# File lib/snogmetrics/kissmetrics_api.rb, line 49 def js! js(reset: true) end
The equivalent of the `KM.record` method of the JavaScript API. You can pass either an event name, an event name and a hash of properties, or only a hash of properties.
# File lib/snogmetrics/kissmetrics_api.rb, line 14 def record(*args) raise 'Not enough arguments' if args.empty? raise 'Too many arguments' if args.size > 2 queue << ['record', *args] end
Register which variant the user saw in an A/B test.
# File lib/snogmetrics/kissmetrics_api.rb, line 42 def set(experiment, variant) queue << ['set', { experiment => variant }] end
The equivalent of the `KM.trackClick` method of the JavaScript API. The first argument should be a selector (a tag id or class name). Furthermore you can pass either an event name, an event name and a hash of properties, or only a hash of properties.
# File lib/snogmetrics/kissmetrics_api.rb, line 34 def trackClick(selector, *args) raise 'Not enough arguments' if args.empty? raise 'Too many arguments' if args.size > 2 queue << ['trackClick', selector, *args] end
Private Instance Methods
# File lib/snogmetrics/kissmetrics_api.rb, line 85 def api_js case @output_strategy when :console_log <<-JS if (window.console) { _kmq = (function(queue) { var printCall = function() { console.dir(arguments); } for (var i = 0; i < queue.length; i++) { printCall.apply(null, queue[i]); } return {push: printCall}; })(_kmq); } JS when :live %(function _kms(e){setTimeout(function(){var s=document,t=s.getElementsByTagName("script")[0],c=s.createElement("script");c.type="text/javascript",c.async=!0,c.src=e,t.parentNode.insertBefore(c,t)},1)}_kms("//i.kissmetrics.com/i.js"),_kms("//scripts.kissmetrics.com/#{@api_key}.2.js");) when :array '' else raise "Unknown KISSmetrics output strategy: #{@output_strategy}" end end
# File lib/snogmetrics/kissmetrics_api.rb, line 81 def queue @session[:km_queue] ||= [] end