I am looking for way to get GTM after clicking button that redirect page to another page

so I am creating a automation to detect if a GTM(dataLayer) is exist or not
but the problem is there is a GTM that emerge when I click login, then, the page get directed to another page immediately. so I don’t get the chance to get the dataLayer (the GTM I am looking just disappear immediately when the page get redirect to another page)

I tried using promise with javascript in katalon it failed. and tried using javascript eventListener but also failed.

can someone help me giving an idea how to solve this problem

Here how I get GTM(dataLayer)

WebUI.executeJavaScript('return dataLayer;', [])

the javascript I tried (but failed)
WebUI.executeJavaScript("document.getElementById('login-step-two-form').addEventListener('click', function(){'return dataLayer' })", Arrays.asList(data))

Sorry, but I don’t think automation software is the way to test this functionality. I cannot think of any way to guarantee consistent results. The JavaScript you try to inject will either not make it in time, or be destroyed at varying moments during the transit from one page to the other. And that transit period is going to vary, moment by moment, every hour of every day.

You would do better to test outcomes. It sounds to me like the ultimate destination is the page you are redirected to – that is the outcome. If you land on the right page, pass the test (step).