Skip to content

Commit

Permalink
Add support for Heap (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanzhang authored and DonSchado committed Nov 8, 2019
1 parent 09b46c6 commit c10ea3e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,18 @@ config.middleware.use(Rack::Tracker) do
end
```
### Heap
[Heap](https://heap.io/). Heap has Projects (e.g. "Main") which have multiple
Environments (e.g. "Production" or "Development"). `env_id` is therefore the numerical ID
that represents the Environment. See Settings -> Projects -> Environments in your dashboard.
```
config.middleware.use(Rack::Tracker) do
handler :heap, env_id: 'HEAP_ID'
end
```
### 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.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require "rack/tracker/bing/bing"
require "rack/tracker/hubspot/hubspot"
require "rack/tracker/drift/drift"
require "rack/tracker/heap/heap"

module Rack
class Tracker
Expand Down
2 changes: 2 additions & 0 deletions lib/rack/tracker/heap/heap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Rack::Tracker::Heap < Rack::Tracker::Handler
end
4 changes: 4 additions & 0 deletions lib/rack/tracker/heap/template/heap.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<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] %>");
</script>
2 changes: 1 addition & 1 deletion lib/rack/tracker/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Rack
class Tracker
VERSION = '1.11.2'
VERSION = '1.12.0'
end
end
10 changes: 10 additions & 0 deletions spec/handler/heap_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe Rack::Tracker::Heap do
def env
{ foo: 'bar' }
end

it 'will be placed in the head' do
expect(described_class.position).to eq(:head)
expect(described_class.new(env).position).to eq(:head)
end
end
17 changes: 17 additions & 0 deletions spec/integration/heap_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'support/capybara_app_helper'

RSpec.describe "Heap Integration" do
before do
setup_app(action: :heap) do |tracker|
tracker.handler :heap, { env_id: '12341234' }
end

visit '/'
end

subject { page }

it 'embeds the script with site_id' do
expect(page).to have_content('heap.load("12341234");')
end
end
4 changes: 4 additions & 0 deletions spec/support/metal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,8 @@ def bing
def drift
render "metal/index"
end

def heap
render "metal/index"
end
end

0 comments on commit c10ea3e

Please sign in to comment.