Project

gtm_rails

0.01
No commit activity in last 3 years
No release in over 3 years
Simply embed Google Tag Manager container snippet into Rails application
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.0.0

Runtime

>= 4.1.0
 Project Readme

gtm_rails Build Status Gem Version git.legal

Simply embed Google Tag Manager container snippet into Rails application.

Installation

Add these lines to your application's Gemfile:

gem 'gtm_rails'

And then execute:

$ bundle install

Configuration

Create config/google_tag_manager.yml in your Rails application.

staging:
  foo: GTM-YourGtmId1
  bar: GTM-YourGtmId2
  baz: GTM-YourGtmId3
production:
  foo: GTM-YourGtmId4
  bar: GTM-YourGtmId5
  baz: GTM-YourGtmId6

The above is a sample. foo, bar, baz are arbitrary label names, and replacing GTM-XXXX with your container ID.

In view, use two helper methods.

  1. google_tag_manager_script_tag(label_name)
  2. google_tag_manager_noscript_tag(label_name)

These method arguments are a label name specified in config/google_tag_manager.yml.

First, following method as close to the opening <head> tag as possible on every page of your website.

<%= google_tag_manager_script_tag(:foo) %>

Google Tag Manager container snippet will be expanded based on Rails.env and label name. If there is no match, a blank string is returned.

<!-- Google Tag Manager -->
<script>(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-YourGtmId1');</script>
<!-- End Google Tag Manager -->

Second, following method immediately after the opening <body> tag on every page of your website.

<%= google_tag_manager_noscript_tag(:foo) %>

Google Tag Manager container snippet will be expanded based on Rails.env and label name. If there is no match, a blank string is returned.

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-YourGtmId1"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

For more detailed GTM please click here to read more.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

gtm_rails is released under the MIT License.