'Does gtag(GA4) takes long time(5s) to fire an event?

I did a test with google analytics event script. This script takes 5 seconds to fire the event.(5 seconds lag):

gtag('event','test',{'event_category':'evnt_cat'});

And this one was fired imediately.

ga('send','event','blabla','a','b')

My questions are: is it true? why does google analytics add lag there? and how can I fire it imediately?

here is the codes:

<head>
<title>test</title>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-*******"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-*******');
</script>
</head>
<body>
<a href='#' onclick="gtag('event','test',{'event_category':'evnt_cat','event_label':'label': true});return false;">test</a>
</body>
</html>```

Thanks for your time.


Solution 1:[1]

I believe ga4 buffers events and waits about 5 seconds or when the page is unloading to fire them using a beacon. This would probably be to avoid interfering with the users' experience and reduce network traffic.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tony McCreath