Google analytics with rails 4

后端 未结 6 1276
后悔当初
后悔当初 2020-12-07 11:39

I am having a bit of difficulty implementing google analytics to my rails 4 project. I included the tracking code to the bottom of my layouts file and have even tried to rem

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-07 12:07

    I use this in my rails app. I just put this in my application.js.erb

    // GA racking code
    (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','https://www.google-analytics.com/analytics.js','ga');
    
    ga('create', 'UA-XXXXX-<%= Rails.env.production? ? '1' : '2' %>', 'auto');
    // I have 2 GA properties- one for debug and another for production
    
    // accommodate Turbolinks and track page views
    $(document).on('ready page:change', function() {
        ga('send', 'pageview');
    });
    

提交回复
热议问题