简体   繁体   中英

Keep google analytics tracking id confidential Rails 4

My question is within Google Analytics how do i keep the ga_tracking_id out of the source code ? I tried to achieve this through an environment variable but the value of the key is in the source for everybody to see. Here is the relevant code.

production.rb

config.ga_tracking_id = ENV['GA_TRACKING_ID']

application.html.erb

<%# Google Analytics (only in production environments) %>
    <% if Rails.env.production? %>
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

          ga('create', '<%= Rails.configuration.ga_tracking_id %>', 'auto');
          ga('send', 'pageview');
        </script>
    <% end %>

Then i set the key configuration at heroku

heroku config:set GA_TRACKING_ID=UA-xxxxxxxx-2

Now the google analytics is working but the tracking id is still in the source. Why is the keys value in the source ? and how can i keep it confidential ?

I am still quite new to application development of any kind, i have been teaching myself through online resources for about 8 months, mainly RoR! Any help would be much appreciated thanks.

It's impossible to hide Google Analytics tracking ID. Google requires it in plain text, so even if it was crypted in some way, any request made to google analytics, easily viewable with chrome dev tools, will contain the GA_TRACKING_ID in plain text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM