Rack Middleware to inject Google Tag Manager code snippet into all html responses with body tag for rack based applications.
Add this line to your application's Gemfile:
gem 'rack_google_tag_manager'
And then execute:
$ bundle install
Add this line to your application's config/application.rb:
config.middleware.use 'Rack::GoogleTagManager', tracker: 'GTM-XXXXXX'
To pass inforamtion to GTM from your application you can do it with a JavaScript array called dataLayer. There are two ways to use it:
dataLayer = [{ 'data': 'myData' }]
dataLayer.push({ 'data': 'myData' })
The first one will overwrite all existing values that were previously passed to the service, while the second will append the new values to the existing ones.
In order for the push
API so be usable, the dataLayer must first be
declared as an array, the GTM JavaScript snippet inserted at the
top of the body will declare it for you.
If your tags need to fire when pages load, but need information beyond URL and referrer, you may need to define the dataLayer and pushes information to it. Add this code above the container snippet so that the data layer information is available on page load. Note that every page must have the code to create a dataLayer and push information to it; the dataLayer does not persist across pages.
- Fork it ( http://github.com/onthebeach/rack_google_tag_manager/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request