Learn business growth with Google Analytics 4 Forums Google Analytics 4 Retrieving event data from Google tag API: A helpful guide

  • Retrieving event data from Google tag API: A helpful guide

    Posted by Mia on 10 April 2023 at 1:15 am

    Hey, so I’m currently playing around with Google tag API to track how many times each offer gets shown to each customer on my e-commerce site. Then, I want to show these vendors how many times their specific offer got viewed, right?

    Now, each time a customer views an offer, I’ve got this going on.

    window.gtag('event', 'offer_view', { 'offer_id': offerID });
    

    The thing is, I am seeing these values popping up on the dashboard, so I know the event is being sent correctly. But the Google Tag documentation isn’t exactly clear on how to fetch all of this event data.

    I gave this a shot:

    window.gtag('get', 'offer_view', 'offer_id', testCallBack);
    

    But the callback function isn’t being called at all. So here’s where I need a bit of help: ideally, I’d rather just pull up a list of offer_id’s with their view counts instead of wading through all the event data. Any tips on where to go from here would be massively appreciated!

    Luke replied 11 months, 1 week ago 3 Members · 2 Replies
  • 2 Replies
  • Aaron

    Administrator
    21 June 2023 at 8:51 pm

    It seems you’re on the right track with monitoring how often customers engage with certain offers on your site. However, there’s a slight snag in the method you’re trying to use. You see, you can’t use the ‘get’ method with gtag unless first you have set data.

    When you are logging an event, like offer_view, the data from that event is essentially outbound – it doesn’t sit around waiting to be fetched, so the get function won’t work here.

    Now, if you want to keep a track of how many times each offer_id gets viewed, then you might typically rest on some good old database work.

    Alternatively, if you are aiming to retrieve these numbers from Google Analytics 4 (GA4), then you would need to use the GA4 Data API. However, this isn’t always the best idea in the given scenario since, by doing so, the API information would be exposed in the Javascript.

    Going through GA4 could still be a useful step to store data and fetch it later on, though, especially if done securely with a 3rd party.

    If you decided you do want to get the data from GA4 Data API, you would provide the following:

    `javascript
    Dimension : customEvent:offer_id
    Metrics : event_count
    Filter : event_name = ${The event with offer_id}
    `

    There are plenty of resources available to help get this set up, such as the following GA4 guides:

    – [GA4 Data Basics](https://developers.google.com/analytics/devguides/reporting/data/v1/basics)
    – [GA4 Data API Schema](https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema)

    Before proceeding with such steps, you’ll want to verify that offer_id is set as an event parameter in your GA4 report.

    That should provide you with everything you need to get started, and hopefully help your business leap towards its next major development. Keep up the great work!

  • Luke

    Member
    9 July 2023 at 2:40 pm

    The gtag.js command “get” that you are trying is not supported, therefore your callback function will not be called. Instead, once gtag.js sends an event to Google Analytics, you are not able to retrieve it directly from the gtag.js API itself. Instead, you may use the Google Analytics Reporting API to fetch this data. The Google Analytics documentation provides guides for this. From here, you should be able to fetch user interactions in the form of events, and sort them by event label (which should correspond to your ‘offer_id’). You can then count the number of times each offer was viewed. Alternatively, in your Google Analytics dashboard, you can create a new report to display event labels and their counts. Infusing this with programming to make the report automatic would require sever-side programming languages such as Python or Java.

Log in to reply.