{"id":29130156,"url":"https://github.com/leehambley/rack-google-analytics","last_synced_at":"2025-06-30T04:03:04.545Z","repository":{"id":56890128,"uuid":"449614","full_name":"kangguru/rack-google-analytics","owner":"kangguru","description":"Simple Rack middleware for implementing google analytics tracking in your Ruby-Rack based project. Supports synchronous and asynchronous insertion and configurable load options","archived":false,"fork":false,"pushed_at":"2019-02-23T03:41:50.000Z","size":579,"stargazers_count":255,"open_issues_count":7,"forks_count":51,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-06-03T18:51:59.228Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kangguru.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-12-26T19:31:29.000Z","updated_at":"2025-05-21T07:13:03.000Z","dependencies_parsed_at":"2022-08-20T16:00:51.464Z","dependency_job_id":null,"html_url":"https://github.com/kangguru/rack-google-analytics","commit_stats":null,"previous_names":["leehambley/rack-google-analytics"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/kangguru/rack-google-analytics","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kangguru%2Frack-google-analytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kangguru%2Frack-google-analytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kangguru%2Frack-google-analytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kangguru%2Frack-google-analytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kangguru","download_url":"https://codeload.github.com/kangguru/rack-google-analytics/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kangguru%2Frack-google-analytics/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262707975,"owners_count":23351529,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-06-30T04:02:10.605Z","updated_at":"2025-06-30T04:03:04.511Z","avatar_url":"https://github.com/kangguru.png","language":"Ruby","readme":"## PLEASE READ:\n\nThere is a new gem for various tracking and analytics services, which includes all the functionality of *rack-google-analytics*.\nSo if you need more than just google, **checkout the [rack-tracker](https://github.com/railslove/rack-tracker) gem**.\n\n---\n\n# Rack google Analytics\n\n[![Build Status](https://travis-ci.org/kangguru/rack-google-analytics.png?branch=analytics-js)](https://travis-ci.org/kangguru/rack-google-analytics)\n\nSimple Rack middleware to help injecting the Google Analytics tracking code in your website.\n\nThis middleware injects the Google Analytics tracking code into the correct place of any request only when the response's `Content-Type` header contains `html` (therefore `text/html` and similar).\n\n## Usage\n\n#### Gemfile\n\n```ruby\ngem 'rack-google-analytics'\n```\n\n#### Sinatra\n\n```ruby\n## app.rb\nuse Rack::GoogleAnalytics, :tracker =\u003e 'UA-xxxxxx-x'\n```\n\n#### Padrino\n\n```ruby\n## app/app.rb\nuse Rack::GoogleAnalytics, :tracker =\u003e 'UA-xxxxxx-x'\n```\n\n#### Rails 3.X and Rails 4.X\n\n```ruby\n## application.rb:\nconfig.middleware.use Rack::GoogleAnalytics, :tracker =\u003e 'UA-xxxxxx-x'\n```\n\n### Options\n\n* `:anonymize_ip` -  sets the tracker to remove the last octet from all IP addresses, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gat?hl=de#_gat._anonymizeIp for details.\n* `:domain`     -  sets the domain name for the GATC cookies. Defaults to `auto`.\n* `:site_speed_sample_rate` - Defines a new sample set size for Site Speed data collection, see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiBasicConfiguration?hl=de#_gat.GA_Tracker_._setSiteSpeedSampleRate\n* `:adjusted_bounce_rate_timeouts` - An array of times in seconds that the tracker will use to set timeouts for adjusted bounce rate tracking. See http://analytics.blogspot.ca/2012/07/tracking-adjusted-bounce-rate-in-google.html for details.\n* `:enhanced_link_attribution` - Enables [Enhanced Link Attribution](https://developers.google.com/analytics/devguides/collection/analyticsjs/advanced#enhancedlink).\n* `:advertising` - Enables [Display Features](https://developers.google.com/analytics/devguides/collection/analyticsjs/display-features).\n* `:ecommerce` - Enables [Ecommerce Tracking](https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce).\n\nIf you are not sure what's best, go with the defaults, and read here if you should opt-out.\n\n## Event Tracking\n\nIn your application controller, you may track an event. For example:\n\n```ruby\nga_track_event(\"Users\", \"Login\", \"Standard\")\n```\n\nSee https://developers.google.com/analytics/devguides/collection/analyticsjs/events\n\n## Custom Push\n\nIn your application controller, you may push arbritrary data. For example:\n\n```ruby\nga_push(\"_addItem\", \"ID\", \"SKU\")\n```\n\n## Dynamic Tracking Code\n\nYou may instead define your tracking code as a lambda taking the Rack environment, so that you may set the tracking code\ndynamically based upon information in the Rack environment. For example:\n\n```ruby\nconfig.middleware.use Rack::GoogleAnalytics, :tracker =\u003e lambda { |env|\n        return env[:site_ga].tracker if env[:site_ga]\n}\n```\n\n## Special use case:  Event tracking only\n\nIf you already set up your Google Analytics `analytics.js` tracker object with pageview tracking in your templates/frontend (inside the `\u003chead\u003e`), the only thing you might want to use the `rack-google-analytics` middleware for is to track server-side events which you can't properly track in the forntend.  In that case simply use the middleware without specifying the `:tracker` option, then it will only render the event tracking code (`ga('send', hitType: 'event', ..)`) and nothing else.\n\n    config.middleware.use Rack::GoogleAnalytics\n\n\n## Thread Safety\n\nThis middleware *should* be thread safe. Although my experience in such areas is limited, having taken the advice of those with more experience; I defer the call to a shallow copy of the environment, if this is of consequence to you please review the implementation.\n\n## Note on Patches/Pull Requests\n\n* Fork the project.\n* Make your feature addition or bug fix.\n* Add tests for it. This is important so I don't break it in a\n  future version unintentionally.\n* Commit, do not mess with rakefile, version, or history.\n  (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)\n* Send me a pull request. Bonus points for topic branches.\n\n## Copyright\n\nCopyright (c) 2009-2012 Lee Hambley. See LICENSE for details.\nWith thanks to [Ralph von der Heyden](https://github.com/ralph) and [Simon Schoeters](https://github.com/cimm) - And the biggest hand to [Arthur Chiu](https://github.com/achiu) for the huge work that went into the massive 0.9 re-factor.\n","funding_links":[],"categories":["Web Apps, Services \u0026 Interaction"],"sub_categories":["Web Analytics"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleehambley%2Frack-google-analytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleehambley%2Frack-google-analytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleehambley%2Frack-google-analytics/lists"}