ahoy_events 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +102 -0
- data/Rakefile +2 -0
- data/ahoy_events.gemspec +25 -0
- data/app/controllers/ahoy/events_controller.rb +10 -0
- data/app/models/ahoy/event.rb +10 -0
- data/config/routes.rb +5 -0
- data/lib/ahoy/subscribers/active_record.rb +17 -0
- data/lib/ahoy/tracker.rb +32 -0
- data/lib/ahoy_events/version.rb +3 -0
- data/lib/ahoy_events.rb +26 -0
- data/lib/generators/ahoy_events/active_record_generator.rb +34 -0
- data/lib/generators/ahoy_events/templates/create_events.rb +20 -0
- data/lib/generators/ahoy_events/templates/initializer.rb +1 -0
- data/vendor/assets/javascripts/ahoy_events.js +25 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4aafe11f432c41f6ba5b0421cff4781e98930519
|
4
|
+
data.tar.gz: af3c865cc2af3ed452c509bbd6d4c6151271f40c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 086ea22e72dfc2f13979ae606402d711726f7228667a86b85328c8eed4bf170d693dd0e2c44ef3953e3c614c272558d88edfc406c025dfc0b741acc8a18d1f81
|
7
|
+
data.tar.gz: 91238c667ebbeed5eeda837251027d82ab5d258d06f20500c6ad3a58f65689df787541b51cec7145eebb66ce8e1e5adab5a597a920f3b4509fc276a0dbe70e2c
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Ahoy Events
|
2
|
+
|
3
|
+
:seedling: Simple, powerful event tracking for Rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
First, [add Ahoy](https://github.com/ankane/ahoy#installation).
|
8
|
+
|
9
|
+
Next, add this line to your application’s Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ahoy_events'
|
13
|
+
```
|
14
|
+
|
15
|
+
Lastly, include the javascript file in `app/assets/javascripts/application.js` after Ahoy.
|
16
|
+
|
17
|
+
```javascript
|
18
|
+
//= require ahoy
|
19
|
+
//= require ahoy_events
|
20
|
+
```
|
21
|
+
|
22
|
+
## How It Works
|
23
|
+
|
24
|
+
Each event has a `name` and `properties`.
|
25
|
+
|
26
|
+
There are three ways to track events.
|
27
|
+
|
28
|
+
#### JavaScript
|
29
|
+
|
30
|
+
```javascript
|
31
|
+
ahoy.track("Viewed book", {title: "The World is Flat"});
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Ruby
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
ahoy.track "Viewed book", title: "Hot, Flat, and Crowded"
|
38
|
+
```
|
39
|
+
|
40
|
+
#### Native Apps
|
41
|
+
|
42
|
+
Send a `POST` request to `/ahoy/events` with:
|
43
|
+
|
44
|
+
- name
|
45
|
+
- properties
|
46
|
+
- user token (depends on your authentication framework)
|
47
|
+
- `Ahoy-Visit` header
|
48
|
+
|
49
|
+
Requests should have `Content-Type: application/json`.
|
50
|
+
|
51
|
+
## Storing Events
|
52
|
+
|
53
|
+
You choose how to store events.
|
54
|
+
|
55
|
+
### ActiveRecord
|
56
|
+
|
57
|
+
Create an `Ahoy::Event` model to store events.
|
58
|
+
|
59
|
+
```sh
|
60
|
+
rails generate ahoy_events:active_record
|
61
|
+
```
|
62
|
+
|
63
|
+
### Custom
|
64
|
+
|
65
|
+
Create your own subscribers in `config/initializers/ahoy.rb`.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class LogSubscriber
|
69
|
+
|
70
|
+
def track(name, properties, options = {})
|
71
|
+
data = {
|
72
|
+
name: name,
|
73
|
+
properties: properties,
|
74
|
+
time: options[:time].to_i,
|
75
|
+
visit_id: options[:visit].try(:id),
|
76
|
+
user_id: options[:user].try(:id),
|
77
|
+
ip: options[:controller].try(:request).try(:remote_ip)
|
78
|
+
}
|
79
|
+
Rails.logger.info data.to_json
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
# and add it
|
85
|
+
Ahoy.subscribers << LogSubscriber.new
|
86
|
+
```
|
87
|
+
|
88
|
+
Add as many subscribers as you’d like.
|
89
|
+
|
90
|
+
## TODO
|
91
|
+
|
92
|
+
- Ability to track JavaScript events automatically (button clicks, etc)
|
93
|
+
- Ability to track Rails events automatically (actions, etc)
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
98
|
+
|
99
|
+
- [Report bugs](https://github.com/ankane/ahoy_events/issues)
|
100
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/ahoy_events/pulls)
|
101
|
+
- Write, clarify, or fix documentation
|
102
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
data/ahoy_events.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ahoy_events/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ahoy_events"
|
8
|
+
spec.version = AhoyEvents::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.summary = %q{Simple, powerful event tracking for Rails}
|
12
|
+
spec.description = %q{Simple, powerful event tracking for Rails}
|
13
|
+
spec.homepage = "https://github.com/ankane/ahoy_events"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "ahoy_matey", ">= 0.1.4"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Ahoy
|
2
|
+
module Subscribers
|
3
|
+
class ActiveRecord
|
4
|
+
|
5
|
+
def track(name, properties, options = {})
|
6
|
+
Ahoy::Event.create! do |e|
|
7
|
+
e.visit = options[:visit]
|
8
|
+
e.user = options[:user]
|
9
|
+
e.name = name
|
10
|
+
e.properties = properties
|
11
|
+
e.time = options[:time]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/ahoy/tracker.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Ahoy
|
2
|
+
class Tracker
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@controller = options[:controller]
|
6
|
+
end
|
7
|
+
|
8
|
+
def track(name, properties, options = {})
|
9
|
+
# publish to each subscriber
|
10
|
+
if @controller
|
11
|
+
options[:controller] ||= @controller
|
12
|
+
end
|
13
|
+
if @controller.respond_to?(:current_user)
|
14
|
+
options[:user] ||= @controller.current_user
|
15
|
+
end
|
16
|
+
if @controller.respond_to?(:current_visit)
|
17
|
+
options[:visit] ||= @controller.current_visit
|
18
|
+
end
|
19
|
+
options[:time] ||= Time.zone.now
|
20
|
+
|
21
|
+
subscribers = Ahoy.subscribers
|
22
|
+
if subscribers.any?
|
23
|
+
subscribers.each do |subscriber|
|
24
|
+
subscriber.track(name, properties, options)
|
25
|
+
end
|
26
|
+
else
|
27
|
+
$stderr.puts "No subscribers"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
data/lib/ahoy_events.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "ahoy_events/version"
|
2
|
+
require "ahoy_matey"
|
3
|
+
require "ahoy/tracker"
|
4
|
+
require "ahoy/subscribers/active_record"
|
5
|
+
|
6
|
+
module AhoyEvents
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Ahoy
|
12
|
+
mattr_accessor :subscribers
|
13
|
+
self.subscribers = []
|
14
|
+
end
|
15
|
+
|
16
|
+
module Ahoy
|
17
|
+
module Controller
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def ahoy
|
22
|
+
@ahoy ||= Ahoy::Tracker.new(controller: self)
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# taken from https://github.com/collectiveidea/audited/blob/master/lib/generators/audited/install_generator.rb
|
2
|
+
require "rails/generators"
|
3
|
+
require "rails/generators/migration"
|
4
|
+
require "active_record"
|
5
|
+
require "rails/generators/active_record"
|
6
|
+
|
7
|
+
module AhoyEvents
|
8
|
+
module Generators
|
9
|
+
class ActiveRecordGenerator < Rails::Generators::Base
|
10
|
+
include Rails::Generators::Migration
|
11
|
+
|
12
|
+
source_root File.expand_path("../templates", __FILE__)
|
13
|
+
|
14
|
+
# Implement the required interface for Rails::Generators::Migration.
|
15
|
+
def self.next_migration_number(dirname) #:nodoc:
|
16
|
+
next_migration_number = current_migration_number(dirname) + 1
|
17
|
+
if ActiveRecord::Base.timestamped_migrations
|
18
|
+
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
|
19
|
+
else
|
20
|
+
"%.3d" % next_migration_number
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def copy_migration
|
25
|
+
migration_template "create_events.rb", "db/migrate/create_ahoy_events.rb"
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_initializer
|
29
|
+
template "initializer.rb", "config/initializers/ahoy.rb"
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :ahoy_events do |t|
|
4
|
+
# visit
|
5
|
+
t.references :visit
|
6
|
+
|
7
|
+
# user
|
8
|
+
t.integer :user_id
|
9
|
+
t.string :user_type
|
10
|
+
|
11
|
+
t.string :name
|
12
|
+
t.text :properties
|
13
|
+
t.timestamp :time
|
14
|
+
end
|
15
|
+
|
16
|
+
add_index :ahoy_events, [:visit_id]
|
17
|
+
add_index :ahoy_events, [:user_id, :user_type]
|
18
|
+
add_index :ahoy_events, [:time]
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Ahoy.subscribers << Ahoy::Subscribers::ActiveRecord.new
|
@@ -0,0 +1,25 @@
|
|
1
|
+
/*jslint browser: true, indent: 2, plusplus: true, vars: true */
|
2
|
+
|
3
|
+
(function (window) {
|
4
|
+
"use strict";
|
5
|
+
|
6
|
+
var ahoy = window.ahoy;
|
7
|
+
|
8
|
+
ahoy.track = function (name, properties) {
|
9
|
+
ahoy.ready( function () {
|
10
|
+
var data = {name: name, properties: properties};
|
11
|
+
ahoy.log(data);
|
12
|
+
|
13
|
+
// ensure JSON is defined
|
14
|
+
if (typeof(JSON) !== "undefined") {
|
15
|
+
$.ajax({
|
16
|
+
type: "POST",
|
17
|
+
url: "/ahoy/events",
|
18
|
+
data: JSON.stringify(data),
|
19
|
+
contentType: "application/json; charset=utf-8",
|
20
|
+
dataType: "json"
|
21
|
+
});
|
22
|
+
}
|
23
|
+
});
|
24
|
+
};
|
25
|
+
}(window));
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ahoy_events
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Kane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ahoy_matey
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple, powerful event tracking for Rails
|
56
|
+
email:
|
57
|
+
- andrew@chartkick.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- ahoy_events.gemspec
|
68
|
+
- app/controllers/ahoy/events_controller.rb
|
69
|
+
- app/models/ahoy/event.rb
|
70
|
+
- config/routes.rb
|
71
|
+
- lib/ahoy/subscribers/active_record.rb
|
72
|
+
- lib/ahoy/tracker.rb
|
73
|
+
- lib/ahoy_events.rb
|
74
|
+
- lib/ahoy_events/version.rb
|
75
|
+
- lib/generators/ahoy_events/active_record_generator.rb
|
76
|
+
- lib/generators/ahoy_events/templates/create_events.rb
|
77
|
+
- lib/generators/ahoy_events/templates/initializer.rb
|
78
|
+
- vendor/assets/javascripts/ahoy_events.js
|
79
|
+
homepage: https://github.com/ankane/ahoy_events
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.2.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Simple, powerful event tracking for Rails
|
103
|
+
test_files: []
|