Tracking Code with Rails Turbolinks


Hi there,

Our website is a Ruby on Rails application using Turbolinks, and we’re trying to implement the Hubspot tracking code. The way turbolinks works when navigating between pages is that instead of doing a full page reload, it simply replaces the of the old page with the <body> of the new page (and any JS in the new page body gets fired once this replacement is complete). Anything in the <head> tag or any JS objects/trackers already instantiated (by the old <body>) are persisted, because the browser doesn’t actually experience a true page reload/refresh when you switch pages.

Since the Hubspot tracking code is implemented directly above the closing body tag, each page reload causes the script to run, which then results in Console warnings like:

duplicate hubspot script with id: "hubspot-messages-loader" included on page
duplicate hubspot script with id: "CollectedForms-XXXXX" included on page

Given this, our concern is whether the tracking code is able to tell when a new page is visited (when using something like Turbolinks to replace page content - as opposed to doing true page reloads)? Alternatively, do we need to implement some kind of manual setup/teardown within Turbolinks whenever a new page is visited (and if so, is this documented anywhere)?