<commit_before>[![Build Status](https://secure.travis-ci.org/zevarito/mixpanel.png)](http://travis-ci.org/zevarito/mixpanel)

## Table of Contents

- [What is Mixpanel] (#what-is-mixpanel)
- [What does this Gem do?] (#what-does-this-gem-do)
- [Install] (#install)
  - [Rack Middleware] (#rack-middleware) 
- [Usage] (#usage)
  - [Initialize Mixpanel class] (#initialize-mixpanel-class) 
- [Examples] (#examples)
 - [How to use it from Rails controllers] (#how-to-use-it-from-rails-controllers)
 - [How to track events using Resque and Rails] (#how-to-track-events-using-resque-and-rails)
- [Supported Platforms] (#supported-platforms)
- [Deprecation Notes] (#deprecation-notes)
- [Collaborators and Maintainers] (#collaborators-and-maintainers)

## What is Mixpanel

Mixpanel is a real-time analytics service that helps companies understand how users interact with web applications.
http://mixpanel.com

## What does this Gem do?

- Track events with properties directly from your backend.
- Track events with properties through javascript using a rack middleware.

## Install

```ruby
  gem install mixpanel
```

### Rack Middleware

*Only need if you want to track events from Javascript.*

If you are using Rails you can add this to your specific environment configuration file (located in config/environments/) or create a new
initializer for it:

```ruby
  config.middleware.use "Mixpanel::Tracker::Middleware", "YOUR_MIXPANEL_API_TOKEN", options
```

Where **options** is a Hash that accepts the following keys:

* **insert_js_last** : true | false

  *Default: false*.
  By default the scripts are inserted into the head of the html response. If you'd prefer the scripts to run after
  all rendering has completed you can set the insert_js_last flag and they'll be added at the end of the body tag.
  This will work whether or not you opt for the aynchronous version of the API. However, when inserting js into an
  ajax response it will have no effect.

* **persist** : true | false

  *Default: false*.
  If you would like, the Mixpanel gem may be configured to store its queue in a Rack session. This allows events
  to be stored through redirects, helpful if you sign in and redirect but want to associate an event with that
  action. The mixpanel gem will also remove duplicate events from your queue for information that should only be
  trasmitted to the API once, such as `mixpanel.identify`, `mixpanel.name_tag`, `mixpanel.people.set`, and
  `mixpanel.register`.
  This allows you to use a before filter to set these variables, redirect, and still have them only transmitted
  once.
  *To enable persistence*, you must set it in both places, Middleware and when you initialize Mixpanel class.

* **config** : a Hash

  *Default: {}*.
  You can also pass Mixpanel configuration details as seen here
  (https://mixpanel.com/docs/integration-libraries/javascript-full-api#set_config)

## Usage

### Initialize Mixpanel class

```ruby
  @mixpanel = Mixpanel::Tracker.new("YOUR_MIXPANEL_API_TOKEN", request.env, true, options)
```
Where **options** is a Hash that accepts the following keys:

* **async** : true | false
  
  *Default: false*.
  Built in async feature. Events are sent to a subprocess via a pipe and the sub process which asynchronously send events to Mixpanel.
  This process uses a single thread to upload events, and may start dropping events if your application generates
  them at a very high rate.
  If you like for a more robust async behavior take a look at Resque example.

* **url** : String
 
  *Default: http://api.mixpanel.com/track/?data=*.
  If you are proxying Mixpanel API requests then you can set a custom url and additionally stop the token from
  being sent by marking it as false if you're going to let the proxy add it.
  Example: { url: "http://localhost:8000/mixpanelproxy?data=" }.

* **persist** : true | false

  *Default: false*.
  If you would like, the Mixpanel gem may be configured to store its queue in a Rack session. This allows events
  to be stored through redirects, helpful if you sign in and redirect but want to associate an event with that<commit_msg>Fixed README Tracker initialization documentation to version 2<commit_after>[![Build Status](https://secure.travis-ci.org/zevarito/mixpanel.png)](http://travis-ci.org/zevarito/mixpanel)

## Table of Contents

- [What is Mixpanel] (#what-is-mixpanel)
- [What does this Gem do?] (#what-does-this-gem-do)
- [Install] (#install)
  - [Rack Middleware] (#rack-middleware) 
- [Usage] (#usage)
  - [Initialize Mixpanel class] (#initialize-mixpanel-class) 
- [Examples] (#examples)
 - [How to use it from Rails controllers] (#how-to-use-it-from-rails-controllers)
 - [How to track events using Resque and Rails] (#how-to-track-events-using-resque-and-rails)
- [Supported Platforms] (#supported-platforms)
- [Deprecation Notes] (#deprecation-notes)
- [Collaborators and Maintainers] (#collaborators-and-maintainers)

## What is Mixpanel

Mixpanel is a real-time analytics service that helps companies understand how users interact with web applications.
http://mixpanel.com

## What does this Gem do?

- Track events with properties directly from your backend.
- Track events with properties through javascript using a rack middleware.

## Install

```ruby
  gem install mixpanel
```

### Rack Middleware

*Only need if you want to track events from Javascript.*

If you are using Rails you can add this to your specific environment configuration file (located in config/environments/) or create a new
initializer for it:

```ruby
  config.middleware.use "Mixpanel::Tracker::Middleware", "YOUR_MIXPANEL_API_TOKEN", options
```

Where **options** is a Hash that accepts the following keys:

* **insert_js_last** : true | false

  *Default: false*.
  By default the scripts are inserted into the head of the html response. If you'd prefer the scripts to run after
  all rendering has completed you can set the insert_js_last flag and they'll be added at the end of the body tag.
  This will work whether or not you opt for the aynchronous version of the API. However, when inserting js into an
  ajax response it will have no effect.

* **persist** : true | false

  *Default: false*.
  If you would like, the Mixpanel gem may be configured to store its queue in a Rack session. This allows events
  to be stored through redirects, helpful if you sign in and redirect but want to associate an event with that
  action. The mixpanel gem will also remove duplicate events from your queue for information that should only be
  trasmitted to the API once, such as `mixpanel.identify`, `mixpanel.name_tag`, `mixpanel.people.set`, and
  `mixpanel.register`.
  This allows you to use a before filter to set these variables, redirect, and still have them only transmitted
  once.
  *To enable persistence*, you must set it in both places, Middleware and when you initialize Mixpanel class.

* **config** : a Hash

  *Default: {}*.
  You can also pass Mixpanel configuration details as seen here
  (https://mixpanel.com/docs/integration-libraries/javascript-full-api#set_config)

## Usage

### Initialize Mixpanel class

```ruby
  @mixpanel = Mixpanel::Tracker.new("YOUR_MIXPANEL_API_TOKEN", request.env, options)
```
Where **options** is a Hash that accepts the following keys:

* **async** : true | false
  
  *Default: false*.
  Built in async feature. Events are sent to a subprocess via a pipe and the sub process which asynchronously send events to Mixpanel.
  This process uses a single thread to upload events, and may start dropping events if your application generates
  them at a very high rate.
  If you like for a more robust async behavior take a look at Resque example.

* **url** : String
 
  *Default: http://api.mixpanel.com/track/?data=*.
  If you are proxying Mixpanel API requests then you can set a custom url and additionally stop the token from
  being sent by marking it as false if you're going to let the proxy add it.
  Example: { url: "http://localhost:8000/mixpanelproxy?data=" }.

* **persist** : true | false

  *Default: false*.
  If you would like, the Mixpanel gem may be configured to store its queue in a Rack session. This allows events
  to be stored through redirects, helpful if you sign in and redirect but want to associate an event with that
{'fraction_non_alphanumeric': 0.08774894387447194, 'fraction_numerical': 0.0010863005431502716, 'mean_word_length': 4.2015065913370995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1477152', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2281, 'n_words': 1073}