简体   繁体   中英

What is the correct way to send a custom event to Google Analytics and capture that data?

I have tried both gtag and ga but no success seeing my custom event results. I managed to see some results under Real-Time but that is not what I want. Nothing shows up under Behavior>Events. I've read that everything is moving to use gtag but all the articles I've read prior to this still use ga so I'm really lost.

The basic set up is that on a button click I want to track the name of the tag or button name that was clicked and keep track of total clicks for that item.

<!-- Not sure if this is the right resource -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-129986551-1"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());


<a class="collection-item" 
 @click.prevent="googleAnalytics('Registry_Tag', tag.name)"> . 
{{tag.name}}</a>


googleAnalytics(category, label){
 ga('send', 'event', category, 'click', label);
}

the output should be {{Tag Category}} > {{tag.name}} = {{number of clicks}}

The use of gtag requires different syntax. Try:

gtag('event', 'click', {
  'event_category': category,
  'event_label': label,
  // 'value': 1
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM