railslove / rack-tracker

Tracking made easy: Don’t fool around with adding tracking and analytics partials to your app and concentrate on the things that matter.
https://www.railslove.com/open-source
MIT License
647 stars 121 forks source link

Avoid injection for AMP pages #146

Closed ghost closed 4 years ago

ghost commented 4 years ago

Love the product, yet run into one very important issues and that's the fact that for AMP pages included javascripts are not allowed. My setup is simple like this

    #configure tracker for website tracking
    config.middleware.use(Rack::Tracker) do
      handler :google_analytics, { tracker: Rails.application.credentials.dig(:google, :ga), ecommerce:true}
    end

I'm looking for a way in the middleware configuration to avoid the injection in case the request.rul end_with .amp Disable by sending DNT=1 in the header works great, yet Google doesn't do that. would love to see a configuration option

DonSchado commented 4 years ago

you can set any tracker option dynamically during requests by providing a lambda.

ghost commented 4 years ago

thanks for the quick response, but can you give an example. When the request is an amp request (mimeType) then it should inject.

DonSchado commented 4 years ago

not sure about the mime type, but you have access to everything in the rack env. have a look here:

DonSchado commented 4 years ago

@petervandeput did that help? mind to share your solution for others?

ghost commented 4 years ago

Not really, problem I'm trying to solve is that when an amp version of the page is request (e.g. by Google) the code is not injected. still haven't figured out how to do

DonSchado commented 4 years ago

I have no experience with AMP but let's figure this out.

Please refer to this spec as an example: https://github.com/railslove/rack-tracker/commit/f3d4e65dee9527136c7613940f5cee16fba91f88#diff-f961e0b587e363c3a6cd9a6a3a231684

You have full access to anything in the rack env hash, which might look something similar to this:

{
  "GATEWAY_INTERFACE"=>"CGI/1.1",
  "HTTP_ACCEPT"=>"text/html,application/xhtml+xml",
  "HTTP_ACCEPT_ENCODING"=>"gzip",
  "HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.8,de;q=0.6",
  "HTTP_CACHE_CONTROL"=>"max-age=0",
  "HTTP_HOST"=>"localhost:3000",
  "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X)",
  "HTTP_VERSION"=>"HTTP/1.1",
  "PATH_INFO"=>"/",
  "QUERY_STRING"=>"",
  "rack.version"=>[1, 3], 
  "rack.url_scheme"=>"http",
  "REMOTE_ADDR"=>"127.0.0.1",
  "REMOTE_HOST"=>"localhost",
  "REQUEST_METHOD"=>"GET",
  "REQUEST_PATH"=>"/"
  "REQUEST_URI"=>"http://localhost:3000/",
  "SCRIPT_NAME"=>"",
  "SERVER_NAME"=>"localhost",
  "SERVER_PORT"=>"3000",
  "SERVER_PROTOCOL"=>"HTTP/1.1",
  "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.1/2015-02-26)",
}

Are you sure the request url contains what you're looking for? or is it maybe the referrer?

So if the env hash is not enough, you should be able to access the controller instance with env["action_controller.instance"] and ask the request object for further information to decide if you inject the tracker.

ghost commented 4 years ago

Thanks solved

bumi commented 4 years ago

@petervandeput do you want to share an example on how you solved it? This might be helpful for others who try to do similar things.

luizeof commented 4 years ago

@petervandeput see https://github.com/railslove/rack-tracker/issues/110#issuecomment-670945283