segment_rails 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -1
- data/lib/segment_rails.rb +24 -3
- data/lib/segment_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3cc0e7317a4be2398f2845fad440ec6f7d6d620
|
4
|
+
data.tar.gz: 0239c02f96c3b938007accd27f3ef852b01c2c0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 874c52ec7cc6db92006887ac2bdf82c883e65395897aa8e60869051dd8833b1d5d7bd11cb7652e1ff3825db63facbd1f6870ba65130e7fdf4c28e41258017e66
|
7
|
+
data.tar.gz: 9178bf6d36c20637850618a001091d1c430453bfb52a195d9854dbdc36364e7d7c45e2ed19ecb397934b74bca0df5f6a626492ee199ebc6bc7bb61e9009dca98
|
data/README.md
CHANGED
@@ -19,7 +19,15 @@ with Segment. Mmmm, now we get the lovely referring domain and utm data!
|
|
19
19
|
## Installation + Usage
|
20
20
|
|
21
21
|
1. Add `segment_rails` to your Gemfile
|
22
|
-
2.
|
22
|
+
2. Configure `segment_rails` in your `ApplicationController`
|
23
|
+
```
|
24
|
+
class ApplicationController < ActionController::Base
|
25
|
+
include SegmentRails
|
26
|
+
set_user_identifier ->() do
|
27
|
+
current_user ? current_user.id : nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
23
31
|
3. Instrument your controllers with `track_event("Timmy fell down the
|
24
32
|
well", { reporter: :lassie })`
|
25
33
|
4. Profit! (Or at least, observe behaviors which you hope will lead to profit)
|
data/lib/segment_rails.rb
CHANGED
@@ -1,12 +1,33 @@
|
|
1
|
+
require 'json'
|
1
2
|
module SegmentRails
|
2
|
-
|
3
|
-
|
3
|
+
if const_defined? :Rails
|
4
|
+
module Rails
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
end
|
4
7
|
end
|
5
8
|
end
|
6
9
|
|
10
|
+
module ClassMethods
|
11
|
+
def set_user_identifier(callback)
|
12
|
+
@user_identifier = callback
|
13
|
+
end
|
14
|
+
|
15
|
+
def user_identifier
|
16
|
+
@user_identifier ? @user_identifier.call() : nil
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.included(klazz)
|
21
|
+
klazz.extend(ClassMethods)
|
22
|
+
end
|
23
|
+
|
24
|
+
def user_identifier
|
25
|
+
self.class.user_identifier
|
26
|
+
end
|
27
|
+
|
7
28
|
def track_event(event_name, properties={})
|
8
29
|
analytics = cookies[:analytics] ? JSON.parse(cookies[:analytics]) : {}
|
9
|
-
analytics[:uuid] =
|
30
|
+
analytics[:uuid] = user_identifier if user_identifier
|
10
31
|
analytics[:events] ||= []
|
11
32
|
analytics[:events].push({ name: event_name, properties: properties})
|
12
33
|
cookies[:analytics] = JSON.dump(analytics)
|