We have introduced Google Analytics to collect data on the web service that we have developed this time and are about to release, so we will introduce how to do it. There is a gem called google-analytics-rails and there is also a way to use it, but when I look at github, the update stopped around October 2017 (Readme was updated last month), so I'm worried So this time I introduced it without gem.
[Prerequisites] -Registered in Google Analytics. (Registration method is here)
Just prepare a partial template for Google Analytics and load it with the head tag.
The point here is the last if statement, which converts current_user to json and passes the data if you are logged in.
It also uses the
`ActionView :: Helpers :: OutputSafetyHelper``` raw method to avoid escaping. You can do the same with html_safe, but it seems that the raw method is recommended as it will raise an exception if it is nil. (Looking at the [Rails Guide](https://guides.rubyonrails.org/active_support_core_extensions.html#output-safety), it says
To insert something verbatim use the raw helper rather tha calling html_safe: `` There is.)
I output what I learned every day! I hope it helps you. If you have any suggestions, I would appreciate it if you could comment.