All threads / How do I install Google tag manager in a rails 5 app?!

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I install Google tag manager in a rails 5 app?!

Muhammad asked in General
Hi guys, I'm having a hard time trying to know how to install GTM in my new rails app. I searched for some time but found nothing to guide me through it!

In your rails layout

Place the following code in the <head></head> tag:
<!-- Google Tag Manager -->
<% if Rails.env.production? %>
<script>
document.addEventListener('turbolinks:load', function(event) {
  var url = event.data.url;  dataLayer.push({
    'event':'pageView',
    'virtualUrl': url
  });
});(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],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');
</script>
<% end %>
<!-- End Google Tag Manager -->

Place the following code in the start of the <body> tag:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

PS.: Don’t forget to replace the “GTM-XXXXXXX” with your Google Tag Manager code.


source: https://gist.github.com/esBeee/545653241530f8f2c2e16371bec56f20
Thanks a lot, Manuel!
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.