Skip to content

Simply embed Google Tag Manager container snippet into Rails application

License

Notifications You must be signed in to change notification settings

ByCodersTec/gtm_rails

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Simply embed Google Tag Manager container snippet into Rails application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 96.2%
  • Logos 3.8%