Skip to content

Commit

Permalink
hotwired/turbo support in GTM
Browse files Browse the repository at this point in the history
  • Loading branch information
wrozka committed Mar 10, 2021
1 parent 0a09c94 commit 20009ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<% end %>
dataLayer.push({'event':'pageView','virtualUrl': url});
});
document.addEventListener('turbo:load', function(event) {
var url = event.detail.url;
<% if events.any? %>
dataLayer.push(<%= events.map(&:write).join(', ') %>);
<% end %>
dataLayer.push({'event':'pageView','virtualUrl': url});
});
<% end %>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
Expand Down
5 changes: 3 additions & 2 deletions spec/integration/google_tag_manager_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
expect(page.find("body")).to have_xpath '//body/noscript/iframe[@src="https://www.googletagmanager.com/ns.html?id=GTM-ABCDEF"]'
end

it "embeds the turbolinks observer if requested" do
it "embeds turbolinks and turbo observers if requested" do
visit '/'
expect(page.find("head")).to_not have_content "turbolinks:load"
setup_app(action: :google_tag_manager) do |tracker|
tracker.handler :google_tag_manager, { container: 'GTM-ABCDEF', turbolinks: true }
end
visit '/'
expect(page.find("head")).to have_content "turbolinks:load"
expect(page.find("head")).to have_content "turbo:load"
end
end
end

0 comments on commit 20009ba

Please sign in to comment.