Skip to content

Commit 0402502

Browse files
Add user identity tracking for Heap
1 parent de0da83 commit 0402502

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@ config.middleware.use(Rack::Tracker) do
627627
end
628628
```
629629
630+
Other options:
631+
632+
* `user_id` - This value will be used as the user identity
633+
* `reset_identity` - A truthy value will reset a user's identity to a random anonymous user ID
634+
630635
### Custom Handlers
631636
632637
Tough we give you handlers for a few tracking services right out of the box, you might

lib/rack/tracker/heap/heap.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
class Rack::Tracker::Heap < Rack::Tracker::Handler
2+
self.allowed_tracker_options = [:user_id, :reset_identity]
23
end
+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
<script>
22
window.heap=window.heap||[],heap.load=function(e,t){window.heap.appid=e,window.heap.config=t=t||{};var r=t.forceSSL||"https:"===document.location.protocol,a=document.createElement("script");a.type="text/javascript",a.async=!0,a.src=(r?"https:":"http:")+"//cdn.heapanalytics.com/js/heap-"+e+".js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n);for(var o=function(e){return function(){heap.push([e].concat(Array.prototype.slice.call(arguments,0)))}},p=["addEventProperties","addUserProperties","clearEventProperties","identify","resetIdentity","removeEventProperty","setEventProperties","track","unsetEventProperty"],c=0;c<p.length;c++)heap[p[c]]=o(p[c])};
33
heap.load("<%= options[:env_id] %>");
4+
<% if tracker_options[:reset_identity] %>
5+
heap.resetIdentity();
6+
<% end %>
7+
<% if tracker_options[:user_id].present? %>
8+
heap.identify("<%= tracker_options[:user_id] %>");
9+
<% end %>
410
</script>

spec/handler/heap_spec.rb

+58
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,62 @@ def env
77
expect(described_class.position).to eq(:head)
88
expect(described_class.new(env).position).to eq(:head)
99
end
10+
11+
describe 'user_id tracker option' do
12+
subject { described_class.new(env, { user_id: user_id }).render }
13+
14+
let(:user_id) { '123' }
15+
16+
it 'will include identify call with user_id value' do
17+
expect(subject).to match(%r{heap.identify\("123"\);})
18+
end
19+
20+
context 'when value is a proc' do
21+
let(:user_id) { proc { '123' } }
22+
23+
it 'will include identify call with the user_id called value' do
24+
expect(subject).to match(%r{heap.identify\("123"\);})
25+
end
26+
end
27+
28+
context 'when value is blank' do
29+
let(:user_id) { '' }
30+
31+
it 'will not include identify call' do
32+
expect(subject).not_to match(%r{heap.identify})
33+
end
34+
end
35+
end
36+
37+
describe 'reset_identity tracker option' do
38+
subject { described_class.new(env, tracker_options).render }
39+
40+
let(:tracker_options) do
41+
{ reset_identity: reset_identity? }.compact
42+
end
43+
44+
context 'when true' do
45+
let(:reset_identity?) { true }
46+
47+
it 'will include resetIdentity call' do
48+
expect(subject).to match(%r{heap.resetIdentity\(\);})
49+
end
50+
end
51+
52+
context 'when false' do
53+
let(:reset_identity?) { false }
54+
55+
it 'will not include resetIdentity call' do
56+
expect(subject).not_to match(%r{heap.resetIdentity\(\);})
57+
end
58+
end
59+
60+
context 'when not given' do
61+
let(:reset_identity?) { nil }
62+
63+
it 'will not include resetIdentity call' do
64+
expect(subject).not_to match(%r{heap.resetIdentity\(\);})
65+
end
66+
end
67+
end
1068
end

spec/integration/heap_integration_spec.rb

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,39 @@
33
RSpec.describe "Heap Integration" do
44
before do
55
setup_app(action: :heap) do |tracker|
6-
tracker.handler :heap, { env_id: '12341234' }
6+
tracker.handler :heap, options
77
end
88

99
visit '/'
1010
end
1111

1212
subject { page }
1313

14+
let(:options) do
15+
{ env_id: '12341234' }
16+
end
17+
1418
it 'embeds the script with site_id' do
1519
expect(page).to have_content('heap.load("12341234");')
1620
end
21+
22+
context 'with user_id tracker option' do
23+
let(:options) do
24+
{ user_id: '345' }
25+
end
26+
27+
it 'includes a call to identify' do
28+
expect(page).to have_content('heap.identify("345");')
29+
end
30+
end
31+
32+
context 'with reset_identity tracker option' do
33+
let(:options) do
34+
{ reset_identity: true }
35+
end
36+
37+
it 'includes a call to resetIdentity' do
38+
expect(page).to have_content('heap.resetIdentity();')
39+
end
40+
end
1741
end

0 commit comments

Comments
 (0)