Learn business growth with Google Analytics 4 Forums Google Analytics 4 Consolidating Multiple Google Tag Custom Events into a Single Request

  • Consolidating Multiple Google Tag Custom Events into a Single Request

    Posted by Ava on 2 August 2022 at 4:09 am

    So, I’m tinkering around with GA4, tracking user views of products on this e-commerce site I’m working on. I’m using this fancy piece of code here:

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

    Then I get all the juicy stats by picking up the event counts for each product ID using Google Analytics data API:

    {
      "dateRanges": [
        {
          "startDate": "2023-04-01"
          "endDate": "2023-05-01"
        }
      ],
      "metrics": [
        {
          "name": "eventCount"
        }
      ],
      "dimensions": [
        {
          "name": "customEvent:offer_id"
        }
      ],
      "dimensionFilter": {
        "filter": {
          "fieldName": "customEvent:offer_id",
          "inListFilter": {
            "values": [
              "75",
              "76",
              "37"
            ]
          }
        }
      }
    }
    

    All’s well that ends well… but does it end well? My users are browsing around, potentially viewing heaps of products, and that’s sending heaps of requests client-side – not cool.

    Any bright ideas on if there’s a way to bundle these same events together and send all the product IDs in one go? I did try sending an array, but as you’d expect, the product ID shows up as an array. Got any tips or tricks for me? Cheers!

    Emma replied 1 year ago 3 Members · 2 Replies
  • 2 Replies
  • Aiden

    Member
    12 April 2023 at 2:27 am

    Hey there! There’s actually no need for you to aggregate these events manually. You see, the awesome part about gtag is that it automatically batches these events for you! So, if you were to fire off a bunch of offer_view events rapidly, you can check your network tab and you’ll see that gtag sends them off in a single request – pretty neat, huh?

    This is because gtag incorporates a slight delay before it sends off a request and it primarily does this to effectively batch your data. There are conditions where gtag will skip this delay, but they’re usually used for debugging purposes.

    You can check out this helpful link for more info on gtag’s batching mechanics:
    [Google Support Link](https://support.google.com/analytics/answer/9322688?hl=en)

    So don’t stress with the manual bundling, gtag’s got your back! Keep tinkering and Happy Coding!

  • Emma

    Member
    19 April 2023 at 2:03 am

    One strategy you might consider is to use a method where you delay the sending of the offer_view event until a certain condition is met – such as a user leaving the page, or after a count of viewed products reaches a certain threshold. Event parameters of GA4 (like offer_id in your use case) do not support passing an array, and hence bundling IDs together in the same event is not directly possible. Consider using localStorage or another method to collect the product IDs client-side as they are viewed. When you’re ready to send the event to GA4, take the collected product IDs, create a string from them (like “75_76_37”), and pass that as the offer_id. This won’t reduce the number of events you’re sending, but it would bundle the offer views together and should lower the number of actual requests to GA4. However, please note that this might complicate your event analysis as multiple offer views will represented as a single event, but with a compound value. You would need to split these values and count them properly in your reports/dashboard.

Log in to reply.