Search code examples
ruby-on-railsgoogle-analyticsserver-side

Rails: Server side Google Analytics all trafic in 'Direct' source


I tried several gems for use Server Side GA.

=> Stacato

=> Gabba

=> ...

But I have always the same problem, in google analytics the SOURCE of all my events are in "Direct" (direct traffic), even if i use utm_source, ...

I remarked, each time I trigger an event, Google analytics create a new session with source "Direct" (and a location in United states), and I think the event is attributed to this session and no to my effective session.

enter image description here

enter image description here

My config (with the gem Gem Gabba https://github.com/hybridgroup/gabba ):

View application.html.erb

<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','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-XXXXXXXX-1', 'auto');
    ga('send', 'pageview');
  </script>

Model User

Gabba::Gabba.new("UA-XXXXXX-1", "mysite.com").event('User', 'Signup', 'Profile completed')

I have a doubt if it's a configuration problem or if it's a recurrent problem when you use a Server Side GA?


Solution

  • Thx @eike & @RaV, you help me to find a solution.

    I removed the gabba gem and added staccato gem instead. The problem was the same (staccato generated a new client_id by default, that's why I had a duplicate), but I figured out how to fix it.

    1.Just save the client_id from in the google analytics cookie in your application controller:

    before_action :tacking_ga
    
    
    def client_id
      cookies["_ga"].split(".").last(2).join(".")
    end
    
      private
    
      def tacking_ga
        @tracker = Staccato.tracker('UA-XXXXXXXXX-1', client_id, ssl: true) if Rails.env == "production"
      end
    

    2.After that in my controller (example user_controller) I just had to add my event at the right place:

    @tracker.event(category: 'User', action: 'Signup', label: "Profile completed", value: nil)
    

    Thx for your help