Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

analytics.js から gtag.js に移行

More than 1 year has passed since last update.

analytics.js から gtag.js に移行

analytics.js から gtag.js へ移行することで google optimize の導入を簡単に済ませたい

今まで使用していた analytics.js

analytics.js
<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','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'TRACKING_ID', 'auto', {'useAmpClientId': true});
  ga('set', 'dimension1', 'DIMENSION1');
  ga('set', 'userId', 'USERID');
  ga('set', 'dimension2', 'DIMENSION2');
</script>

カスタムディメンションを2つ, また userid を導入している。

新しく導入した gtag.js

gtag.js
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=TRACKING_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('set', 'dimension1', 'DIMENSION1');
  gtag('set', 'user_id', 'USERID');
  gtag('set', 'dimension2', 'DIMENSION2');
  gtag('config', 'TRACKING_ID', {
       'optimize_id': 'OPTIMIZE_TRACKING_ID>'
      });
</script>

ga() で渡していたそれぞれの値は gtag で渡すように修正
gtag('config') で optimize_id として tracking_id を入れることで簡単に google optimize を導入することができた。

参考資料

新しいGA「gtag.js」を使うとGoogle Optimize実践まで超簡単だった
https://qiita.com/kent_ear/items/04304753eb1f8c972b54

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away