Search code examples
google-analyticsgoogle-tag-managergoogle-analytics-apigoogle-analytics-4

How to get event value from Google tag API


I am using Google tag API to count the number of times an offer is displayed on a customer's screen (impression) for an e-commerce website. I will then show the total view count (impression) of each offer to the vendors.

I call the below each time an offer is viewed by a customer.

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

I can see these values on the dashboard so the event is sent properly. However, the documentation does not specify something to fetch the overall event data.

I tried

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

but the callback function is not even called.

I also would prefer to present a list of offer_id and get view counts for those rather than loading the whole event data if possible. I would be grateful if you could provide some guidance/pointers.


Solution

  • The gtag get is only available when you set something first.

    If you data is provided with event then it can not be get.


    This offer_view idea should be done through some database work.

    Or you want to use the number from Google Analytics 4?

    Then you need to use the GA4 Data API but still not a good choice on this scenario because you will expose the API information on the JS.

    Overall, you need a 3rd party like GA4 to store the data and can be retrieved safely.


    If you want to get the data from GA4 Data API

    Just give the

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

    Please follow some document

    https://developers.google.com/analytics/devguides/reporting/data/v1/basics

    https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema

    Check the GA4 rerpot offer_id should already be set as an event parameter.