[Ruby] Perform site analysis using Google Analytics-Chapter 1

1 minute read

background

I’m wondering if the site I’m currently deploying to is being viewed by users other than myself, so I decided to introduce Google Analytics in order to investigate the history and number of visits (I feel like biting a bit…lol) Until now, I have checked the access to my portfolio site from git traffic etc., but I would like to investigate various trends using analytics.

  • About 1 hour has passed since the setting of Analytics for this article. I don’t see the analysis results yet, but I’d like to wait a bit longer before reporting the situation (it takes 24 hours at the longest). We will post the display results and correction information of the procedure.

Environment

| Item | Content | |:—————–|:——————:| | OS.Amazon Linux AMI | release 2018.03 | | Ruby On Rails | v5.2.4.3 | | MySQL | v5.6 |

Setting procedure

Working time: 15 minutes (1) Creating a Google account (Omitted) (2) Create a Google Analytics account According to the site, mainly just enter the following: ·account name ・Website name ・Website URL · Report time zone

(3) Install tracking code A tracking code will be distributed when you create the above account. Include this in the <header> tag on each page of your site. In my case, it’s running on rails, so I set it to application.html.erb. It will be as follows.

erb:app/views/layouts/application.html.erb


<html>
  <head>
   ...

    <!-- Global site tag (gtag.js)-Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxx-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config','UA-xxxxxxxx-1');
    </script>

  </head>

  <body>
    <%= yield %>
  </body>
</html>

that’s all.

Reference

Setting and installation method when introducing Google Analytics [for beginners] [Can’t ask now] What is Google Analytics? Understand the installation procedure and usage in 5 minutes!