最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Will dataLayer.push() definitely send data to google when triggered on an anchor? - Stack Overflow

programmeradmin7浏览0评论

This may seem like a simple question, but it doesn't seem to be answered anywhere that i can find.

I am writing an onClick event handler that simply calls dataLayer.push() when an anchor is clicked.

Is dataLayer.push() a synchronous operation?

Will the GET request to google definitely be sent, even though the browser has unloaded the page it was requested from due to the link being followed?

Some browsers show the connection get cancelled, some show it success.

My question is if the puter is slow, is it possible for the page to get unloaded before the request is sent?

This is why i assume that google started using the eventCallback property to redirect the user after the link has been followed. e.g.

This source code does not include the click handler, but implies that the onClick event should stop propogation and let the eventCallback function set document.location.

However, as soon as you cancel the event, all its information has gone.

This (in my opinion) is just the wrong way to do it. e.g. (CTRL or COMMAND) + Click opens a new tab on browsers. This will not work unless the onClick event handler allows the prorogation to continue.

Relying on eventCallback also means that if the google scrips didn't load for one of the many reasons it could (but is still unlikely), your links don't work. And your site is broken.

So this leaves the correct way to do it for the onClick event handler to allow the event to propagate and return true.

Which also means that dataLayer.push() would need return after the GET request was sent for any of this to work properly.

Code example: NOTE: You will get mixed results in mixed environments.

<a href="/somewhere-else">Link</a>


$(document).on('click', 'a', function(event) {

    // Is dataLayer.push() guaranteed to fire a GET ?
    // data set externally
    dataLayer.push(data);

    return true;

});

Is there anyone out there that can guarantee that the GET request will get fired to the google server?

Have the google developers forgotten something here?

EDIT: Updated title to be more relevant to the question.

This may seem like a simple question, but it doesn't seem to be answered anywhere that i can find.

I am writing an onClick event handler that simply calls dataLayer.push() when an anchor is clicked.

Is dataLayer.push() a synchronous operation?

Will the GET request to google definitely be sent, even though the browser has unloaded the page it was requested from due to the link being followed?

Some browsers show the connection get cancelled, some show it success.

My question is if the puter is slow, is it possible for the page to get unloaded before the request is sent?

This is why i assume that google started using the eventCallback property to redirect the user after the link has been followed. e.g. https://developers.google./tag-manager/enhanced-emerce#product-clicks

This source code does not include the click handler, but implies that the onClick event should stop propogation and let the eventCallback function set document.location.

However, as soon as you cancel the event, all its information has gone.

This (in my opinion) is just the wrong way to do it. e.g. (CTRL or COMMAND) + Click opens a new tab on browsers. This will not work unless the onClick event handler allows the prorogation to continue.

Relying on eventCallback also means that if the google scrips didn't load for one of the many reasons it could (but is still unlikely), your links don't work. And your site is broken.

So this leaves the correct way to do it for the onClick event handler to allow the event to propagate and return true.

Which also means that dataLayer.push() would need return after the GET request was sent for any of this to work properly.

Code example: NOTE: You will get mixed results in mixed environments.

<a href="/somewhere-else">Link</a>


$(document).on('click', 'a', function(event) {

    // Is dataLayer.push() guaranteed to fire a GET ?
    // data set externally
    dataLayer.push(data);

    return true;

});

Is there anyone out there that can guarantee that the GET request will get fired to the google server?

Have the google developers forgotten something here?

EDIT: Updated title to be more relevant to the question.

Share Improve this question edited Feb 18, 2017 at 0:01 Leon V asked Feb 16, 2017 at 1:19 Leon VLeon V 1,2331 gold badge9 silver badges8 bronze badges 1
  • While I think this is a good question for a discussion (and have offered an answer) SO is generally not a discussion forum, so to ask if a vendor's solution is a good one will probably be closed as off-topic. You might want to rephrase this as "how can I make sure my data is sent". – Eike Pierstorff Commented Feb 19, 2017 at 9:53
Add a ment  | 

2 Answers 2

Reset to default 11

datalayer.push does not send anything to Google. It pushes objects with key/value pairs to the datalayer array. This might contain an event which in turn fires a tag. Whether the tag is sent depends on the setup of the tag, not on the dataLayer.push.

As a consequence, when you write your own click handlers your are yourself responsible to make sure your tags are actually fired.

If you use the built-in click handler you can configure a delay to make sure your tag has time to fire before the link redirects:

Since link clicks usually cause the browser to load a new page and interrupt any pending HTTP request, you have the option to add a small delay to allow tags fired by Tag Manager to execute properly before redirecting to the next page. Checking the “Wait For Tags” option will delay opening of links until all tags have fired or the specified timeout has elapsed, whichever es first.

You should be able to mix both methods (push data on the click, but still use the "native" link click handler for the event).

You can also try to specify "beacon" as the transport method in your Google Analytics tags, on browsers that support this (which I think is only Chrome at the moment) GA will then use the navigator.sendBeacon interface, which sends the data even in case the page unloads.

You might think that Google's solution is not very elegant (but the simple delay has the advantage that it works for all tags, not just for GA), but they have not "forgotten" the problem.

Also solutions that bine GA hit callbacks with timeouts that redirects if the callback fails as proposed i.e. by Simo Ahava somewhere should be be doable with GTM, even if they are probably more cumbersome to implement in GA.

I appreciate this is a very old question, but an update from 2025. I had an issue where the Default Consent wasn't being processed as expected.

// This doesn't work as expected:
dataLayer.push([
  "consent",
  "default",
  { ad_storage: "denied", analytics_storage: "denied", functionality_storage: "denied", personalization_storage: "denied", security_storage: "denied" }
  ]);

// This does:
gtag = function () { dataLayer.push(arguments) };
gtag(
  "consent",
  "default",
  { ad_storage: "denied", analytics_storage: "denied", functionality_storage: "denied", personalization_storage: "denied", security_storage: "denied" }
  );

Why, because it expected an arguments object, which "is an array-like object". Emphasis on "like".

The problem is that in the developer console the two object look very alike so I assumed the dataLayer.push was correct. It was only when I checked pared the two that I realised the difference. Note the difference, the value pushed using gtag has a gtm.uniqueEventId property (amongst others).

发布评论

评论列表(0)

  1. 暂无评论