Skip to content

Add user identity tracking for Heap #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,11 @@ config.middleware.use(Rack::Tracker) do
end
```

Other options:

* `user_id` - This value will be used as the user identity
* `reset_identity` - A truthy value will reset a user's identity to a random anonymous user ID

### Custom Handlers

Tough we give you handlers for a few tracking services right out of the box, you might
Expand Down
1 change: 1 addition & 0 deletions lib/rack/tracker/heap/heap.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Rack::Tracker::Heap < Rack::Tracker::Handler
self.allowed_tracker_options = [:user_id, :reset_identity]
end
6 changes: 6 additions & 0 deletions lib/rack/tracker/heap/template/heap.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<script>
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])};
heap.load("<%= options[:env_id] %>");
<% if tracker_options[:reset_identity] %>
heap.resetIdentity();
<% end %>
<% if tracker_options[:user_id].present? %>
heap.identify("<%= tracker_options[:user_id] %>");
<% end %>
</script>
58 changes: 58 additions & 0 deletions spec/handler/heap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,62 @@ def env
expect(described_class.position).to eq(:head)
expect(described_class.new(env).position).to eq(:head)
end

describe 'user_id tracker option' do
subject { described_class.new(env, { user_id: user_id }).render }

let(:user_id) { '123' }

it 'will include identify call with user_id value' do
expect(subject).to match(%r{heap.identify\("123"\);})
end

context 'when value is a proc' do
let(:user_id) { proc { '123' } }

it 'will include identify call with the user_id called value' do
expect(subject).to match(%r{heap.identify\("123"\);})
end
end

context 'when value is blank' do
let(:user_id) { '' }

it 'will not include identify call' do
expect(subject).not_to match(%r{heap.identify})
end
end
end

describe 'reset_identity tracker option' do
subject { described_class.new(env, tracker_options).render }

let(:tracker_options) do
{ reset_identity: reset_identity? }.compact
end

context 'when true' do
let(:reset_identity?) { true }

it 'will include resetIdentity call' do
expect(subject).to match(%r{heap.resetIdentity\(\);})
end
end

context 'when false' do
let(:reset_identity?) { false }

it 'will not include resetIdentity call' do
expect(subject).not_to match(%r{heap.resetIdentity\(\);})
end
end

context 'when not given' do
let(:reset_identity?) { nil }

it 'will not include resetIdentity call' do
expect(subject).not_to match(%r{heap.resetIdentity\(\);})
end
end
end
end
26 changes: 25 additions & 1 deletion spec/integration/heap_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,39 @@
RSpec.describe "Heap Integration" do
before do
setup_app(action: :heap) do |tracker|
tracker.handler :heap, { env_id: '12341234' }
tracker.handler :heap, options
end

visit '/'
end

subject { page }

let(:options) do
{ env_id: '12341234' }
end

it 'embeds the script with site_id' do
expect(page).to have_content('heap.load("12341234");')
end

context 'with user_id tracker option' do
let(:options) do
{ user_id: '345' }
end

it 'includes a call to identify' do
expect(page).to have_content('heap.identify("345");')
end
end

context 'with reset_identity tracker option' do
let(:options) do
{ reset_identity: true }
end

it 'includes a call to resetIdentity' do
expect(page).to have_content('heap.resetIdentity();')
end
end
end