Jirafe Analytics Ruby Client

Build Status Code Climate

This is the official Jirafe rubygem. It fully wraps Jirafe's API.

This client library can be used to add Jirafe analytics to your eCommerce software.

Installation

Note: If you are using ruby 1.8.x. You will also need to install a JSON library.

gem install jirafe

Or, add the jirafe gem to your project's Gemfile

gem "jirafe"

Then from your project's root, run:

bundle

If you're using Jirafe in a rails app, drop this into config/initializers/jirafe.rb:

Jirafe.configure do |config|
  config.token  = '12345abcde'                # required
  config.logger = Logger.new('/dev/null')     # defaults to Rails.logger or Logger.new(STDOUT).
                                              # Set to Logger.new('/dev/null') to disable logging.
end

Use

Each resource in the Jirafe API is represented by an object e.g:

  • Jirafe::Resource::Application
  • Jirafe::Resource::Site
  • Jirafe::Resource::Sites::Products

To register a new application with Jirafe:

application = Jirafe::Resource::Application.create!(:name => "My New Account")

To get all sites associated with this application:

application.sites

To send order data events:

site = application.sites.first
Jirafe::Callback::Event.notify(site.identifier)

# Jirafe will send a GET request to your site's store_api_url with a confirmToken parameter
token = params['confirmToken']

event = Jirafe::Callback::Events::Order.new(:version => 1,
                                            :action => :create,
                                            :identifier => 1,
                                            :grand_total => 100.00,
                                            :sub_total => 80.00,
                                            :tax_amount => 20.00,
                                            :discount_amount => 0.00,
                                            :status => :pending)
Jirafe::Callback::Event.send(site.identifier, token, [event])

To place javascript tags for visit tracking:

data = { "id" => "999" } # 999 is your site id
Jirafe::Tracker.js_tag(data.to_json)

Which will generate for placement on a page:

<script type='text/javascript' id='jirafe_analytics'>
  var jirafe = {"id":"999"};
  (function(){
  var d=document,g=d.createElement('script'),s=d.getElementsByTagName('script')[0];
  g.type='text/javascript',g.defer=g.async=true;g.src=d.location.protocol+'//c.jirafe.com/jirafe.js';
  s.parentNode.insertBefore(g, s);
  })();
</script>

Development

Specs

The Jirafe gem uses rspec 2 for tests. To run the test suite, just type rake or bundle exec rake spec in the gem's base directory.

For troubleshooting you can set the following environment variables:

DEBUG_JIRAFE=true # prints out httparty details
CALLTHROUGH=true # Ignore VCR fixtures and call through to underlying service. For internal development only.

Contributing

  1. Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  2. Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  3. Fork the project.
  4. Start a feature/bugfix branch
  5. Commit and push until you are happy with your contribution. Make sure to add tests so the change does not cause a regression.
  6. Send a pull request.

Notes

Test on Ruby 1.9.3-p194 and Ruby 1.8.7-p370

License

Copyright 2012, Jirafe, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.