[Rails] How to introduce Google Analytics [Easy]

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)

Introduction method

Just prepare a partial template for Google Analytics and load it with the head tag.

haml:layauts/_google_analytics.html.haml


%script{async: "", src: "https://www.googletagmanager.com/gtag/js?id=Tracking ID"}
:javascript
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'Tracking ID');
  if (#{raw current_user.to_json}) {
    gtag('set', {'user_id': #{raw current_user.to_json}.id});
  }

haml:application.html.haml


!!!
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}
    = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'
    - if Rails.env.production?
      = render 'layouts/google_analytics'

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.)

reference

https://qiita.com/t1gert1ger/items/b9a197e2d85050b9d7d6

Thank you for reading to the end!

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.

Recommended Posts

[Rails] How to introduce Google Analytics [Easy]
How to use Google Colaboratory
Export Google Analytics Standard to BigQuery
[Rails] Set Google Analytics conversion tag
[Memo] How to use Google MµG
How to update Google Sheets from Python
How to use Google Test in C
How to use Google Assistant on Windows 10
How to search Google Drive with Google Colaboratory
[Rails] google maps api How to post and display including map information
How to display Map using Google Map API (Android)
How to implement Rails helper-like functionality in Django
Python + chatwork + google extension = How to make an easy and funny chat BOT
How to use the Google Cloud Translation API
How to display videos inline in Google Colab
[Rails] How to display multiple markers on Google Map and display a balloon when clicked
[Rails] How to get location information using Geolocation API
How to run AutoGluon in Google Colab GPU environment
How to load files in Google Drive with Google Colaboratory
How to install Google Test / Google Mock in Visual Studio 2019
Easy way to scrape with python using Google Colab
How to run Self bot on Discord.py [Easy vandalism! ]
How to analyze with Google Colaboratory using Kaggle API
How to use Django on Google App Engine / Python
How to use Spacy Japanese model in Google Colaboratory
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
Introduce serverspec to Linux
How to use pytest_report_header
Easy to use Flask
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv