Skip to content

Commit 743b621

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

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ 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+
630634
### Custom Handlers
631635
632636
Tough we give you handlers for a few tracking services right out of the box, you might

lib/rack/tracker/heap/heap.rb

Lines changed: 1 addition & 0 deletions
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]
23
end
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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[:user_id].present? %>
5+
heap.identify("<%= tracker_options[:user_id] %>");
6+
<% end %>
47
</script>

spec/handler/heap_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,30 @@ 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 'with user_id tracking' 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 user_id 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 user_id 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
1036
end

0 commit comments

Comments
 (0)