lundi 4 mars 2019

How to track users in Rails using Google Analytics

I am using the following google analytics code to track users in my Ruby on Rails 4 app

    // ga('create', 'UA-1234567-1', 'auto');
    ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.id %> });
    // ga('set', '&uid', <%= current_user.id %>); // Set the user ID using signed-in user_id.
    ga('send', 'pageview');

but I am getting the error

undefined method `id' for nil:NilClass

for the line

ga('create', 'UA-1234567-1', 'auto', { 'userId': <%= current_user.id %> });

I am following this thread How do I set USER_ID in Google Universal Analytics tracking code in Rails? and official doc https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id but not sure what is going wrong as it is almost same code.

Aucun commentaire:

Enregistrer un commentaire