Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasc committed Aug 13, 2014
1 parent 470a6d4 commit 8d54f2a
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,108 @@ Aggregated traffic logs stored in MongoDB. Based on the approach described by Jo

Add this line to your application's Gemfile:

gem 'mongoid_traffic'
```Ruby
gem 'mongoid_traffic'
```

And then execute:

$ bundle
```
$ bundle
```

Or install it yourself as:

$ gem install mongoid_traffic
```
$ gem install mongoid_traffic
```

## Usage

Log your traffic like this:

Mongoid::TrafficLogger.log
```Ruby
Mongoid::TrafficLogger.log
```

Or, in case of Rails, you can use the `.after_action` macro with the `#log_traffic` helper method in your controllers:

class MyController < ApplicationController
after_action :log_traffic, only: [:show]
end
```Ruby
class MyController < ApplicationController
after_action :log_traffic, only: [:show]
end
```

### Scope

You can scope the log using the (optional) `scope:` argument:

Mongoid::TrafficLogger.log(scope: '/pages/123')
```Ruby
Mongoid::TrafficLogger.log(scope: '/pages/123')
```

Or, in case of Rails controller:

class MyController < ApplicationController
after_action :log_scoped_traffic, only: [:show]
end
```Ruby
class MyController < ApplicationController
after_action :log_scoped_traffic, only: [:show]
end
```

By default, the `:log_scoped_traffic` method scopes your log by the request path (for example '/pages/123'). You can override this behavior with custom scope like this:

class MyController < ApplicationController
after_action :log_scoped_traffic, only: [:show]
```Ruby
class MyController < ApplicationController
after_action :log_scoped_traffic, only: [:show]

private
def log_scoped_traffic
super.log_scoped_traffic(scope: 'my-scope-comes-here')
end
private

def log_scoped_traffic
super.log_scoped_traffic(scope: 'my-scope-comes-here')
end
end
```

It might be good idea to use both methods in order to log access to the whole site as well as access to individual pages:

class MyController < ApplicationController
after_action :log_traffic, only: [:show]
after_action :log_scoped_traffic, only: [:show]
end
```Ruby
class MyController < ApplicationController
after_action :log_traffic, only: [:show]
after_action :log_scoped_traffic, only: [:show]
end
```

## Accessing the log

### Access count

The total number of views within a specific month can be accessed like this:

Mongoid::TrafficLog.for_year(2014).for_month(8).access_count
```Ruby
Mongoid::TrafficLog.for_year(2014).for_month(8).access_count
```

The total number of views per scope per specific date like this:

Mongoid::TrafficLog.for_date(Date.today).access_count
```Ruby
Mongoid::TrafficLog.for_date(Date.today).access_count
```

### User Agent

Optionally, you can pass 'User-Agent' header string to the logger:

Mongoid::TrafficLogger.log(user_agent: user_agent_string)
```Ruby
Mongoid::TrafficLogger.log(user_agent: user_agent_string)
```

### Referer

Optionally, you can pass 'User-Agent' header string to the logger:

Mongoid::TrafficLogger.log(referer: http_referer_string)
```Ruby
Mongoid::TrafficLogger.log(referer: http_referer_string)
```

## Contributing

Expand Down

0 comments on commit 8d54f2a

Please sign in to comment.