How to add event listener?

Not understand the event listener, so I'm not sure whether it will solve my problem.

There is an event sent to GA:
gtag('event', 'event Name', { 'event_category': "Lead", 'event_label': document.location.href });

How can I add event listener when the function is executed gtag, which has a parameter 'event' equal 'name1'?

Ultimately, I need to write a script that when running I need gtag will cause alert();
April 4th 20 at 00:35
2 answers
April 4th 20 at 00:37
He gtag does not listen to events on it. Therefore, it is necessary to go through all your code and find all the calls gtag() method with the same event. To add your alert()
Or make a layer for your dimensions. On the page to call instead of the gtag() function, for example, mytag() which, in turn, will cause gtag() and maybe other scripts analysts.

Upd. in the comments it turned out that the vehicle can not change the calls to gtag() In this case, is to replace the function gtag() on his:
function gtag(){
 dataLayer.push(arguments); // this is the whole source of her functionality

 if (arguments.length > 1
 && typeof arguments[0] === 'string'
 && arguments[0].toLowerString() === 'event'
 && typeof arguments[1] === 'string'
 && arguments[1] === 'name1'
)
{
 alert("Name 1!");
}
}
we are talking about an external plug-in script whose source code I can't rewrite(
sorry, realized - Misty.Anders commented on April 4th 20 at 00:40
Option to intercept calls to global funktsii gtag(), replacing it with his own.
The difficulty is that its definition is loaded using <script async ...> and there's no way to catch the moment of loading and determination of this feature, you will have hostility. - jack25 commented on April 4th 20 at 00:43
Question: what can you rewrite? The HTML of the page itself you can? - jack25 commented on April 4th 20 at 00:46
@jack25, but a person below threw the working code, it triggered an alert.
just can't understand why ceases to work when I'm not directly in the console paste the code and the source code of your website..

/* original function */
function gtag() {
 console.log('args: ', arguments);
}

__gtag = gtag;

gtag = function() {
 if (arguments[0] === 'name1') {
alert(42);
}

 __gtag.apply(this, arguments);
}
- Misty.Anders commented on April 4th 20 at 00:49
April 4th 20 at 00:39
Try this:
/* original function */
function gtag() {
 console.log('args: ', arguments);
}

__gtag = gtag;

gtag = function() {
 if (arguments[0] === 'name1') {
alert(42);
}

 __gtag.apply(this, arguments);
}


UPD:
And, well, here's the documentation written about the event_callback https://developers.google.com/gtagjs/reference/par...
It doesn't help?
eh, this parameter need to pass along with the call gtag('event', {...})
T. S. writes that the challenges he cannot influence. Therefore, it remains only to replace the global function gtag() on your own. - jack25 commented on April 4th 20 at 00:42
Maxim, whoa! here is the first code seems to be what you need.
I put in the console of chrome and alert earned.

but when put directly into the code of the site, it is in the console nothing appears and the alert is not triggered.
this code is only to insert into the console is running or in an html paste also should work? - Misty.Anders commented on April 4th 20 at 00:45

Find more questions by tags JavaScript