Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@nextjs/third-party/google Custom Events Don't Register When Using sendGAEvent() #61703

Closed
AbdelrahmanSadat opened this issue Feb 6, 2024 · 12 comments · Fixed by #62065 or #62192
Closed
Labels
bug Issue was opened via the bug report template. locked

Comments

@AbdelrahmanSadat
Copy link

AbdelrahmanSadat commented Feb 6, 2024

Link to the code that reproduces this issue

https://github.com/AbdelrahmanSadat/ga-issue

To Reproduce

  1. Use <GoogleAnalytics> component in the app's layout.
  2. Use sendGAEvent({event: "custom_event", value: 'value'}) on any client component.
  3. Add the custom_event in GA
  4. Trigger the custom_event
  5. Check Google Analytics realtime dashboard. No event is reported.

Current vs. Expected behavior

Expected the custom_event to be reported in GA.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
Binaries:
  Node: 20.10.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.3
  eslint-config-next: 14.0.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Script optimization (next/script)

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Not really sure how I can provide a reproduction for this since it would also require access to a Google Analytics dashboard. But long story short, I tried @nextjs/third-parties/google instead of using a <Script>, and while that part works and normal events fire, manually firing custom events (or predefined events) using the library's SendGAEvent() doesn't register on the dashboard.

I removed any use of SendGAEvent() and instead used (window as any).gtag("event", "custome_event", {value: "value"}), and my custom_event was firing and showing on the dashboard, so that rules out any issue in my GA setup.

@AbdelrahmanSadat AbdelrahmanSadat added the bug Issue was opened via the bug report template. label Feb 6, 2024
@Mhmdrza
Copy link
Contributor

Mhmdrza commented Feb 6, 2024

In my case the /collect request is not even sent to ga.

expected behavior:

  • sendGAEvent triggers the /collect request

@honzasladek
Copy link

I encountered this as well today. Behaves exactly the same as described here in the issue for me.

@MinhNgyuen
Copy link

Holy thank you for posting this. I've been going crazy trying to use this package to send custom events.

@benlhachemi
Copy link

same thing for me, default events are working fine (scroll, view, etc...) but custom ones are not !

@mulfyx
Copy link

mulfyx commented Feb 12, 2024

@willashe
Copy link
Contributor

willashe commented Feb 14, 2024

Running into the same issue. It appears to be due to how arguments are being read and passed to the dataLayer. The window.gtag function passes the arguments array-like object (i.e. an object with numbered keys, not an array) directly into the dataLayer, which results in the events showing up in the realtime dashboard as expected. sendGAEvent uses the rest parameter syntax and passes the resulting actual array into the dataLayer, which seems to result in no event being sent to GA. Seems GA is specifically looking for that array-like object and not an actual array.

ijjk added a commit that referenced this issue Feb 16, 2024
Previous implementation of `sendGAEvent` was pushing arguments to
`dataLayer` via rest parameter syntax, resulting in an actual array,
which GA doesn't seem to process correctly (resulting in the event not
showing up in GA reports). This PR refactors it to a vanilla JS function
passing data via the `arguments` array-like object, which is able to be
processed correctly and results in the event showing up as expected.

fixes [61703](#61703)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@asobirov
Copy link
Contributor

asobirov commented Feb 17, 2024

I believe the merged code does not fix the issue as it still can be seen with "@next/third-parties": "^14.1.1-canary.58". As per @willashe's comment, seems like dataLayer doesn't like Arrays?

Left a comment in the PR: https://github.com/vercel/next.js/pull/62065/files/4572a3bbd70c5c42fcf0c70bc90b7d936605a702#r1493345098

ijjk pushed a commit that referenced this issue Feb 17, 2024
The recently merged PR - #62065,
doesn't seem to fix the issue. As per the comment this seems to be
related to `dataLayer` that does not like working with Arrays, but works
with [arguments
object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments).
Also, when using window.gtag(...) an Arguments() is pushed instead of
Array().

Fixes #61703
@willashe
Copy link
Contributor

willashe commented Feb 19, 2024

I believe the merged code does not fix the issue as it still can be seen with "@next/third-parties": "^14.1.1-canary.58". As per @willashe's comment, seems like dataLayer doesn't like Arrays?

Left a comment in the PR: https://github.com/vercel/next.js/pull/62065/files/4572a3bbd70c5c42fcf0c70bc90b7d936605a702#r1493345098

Yea disappointing that my changes were reverted and merged, the resulting PR didn't fix anything. Thank you for picking this back up anyway.

@felix-mittermeier
Copy link

Hi guys, I am a bit confused now what the latest status is. Can you confirm that the second merge ("Fix sendGAEvent function (for real?)") really solves the issue? I built my app using the 14.1.1-canary.61 version but the events still don't show up in Google Analytics. Also not in the real time view.

Maybe I understood something wrong and another fix is needed first, then any clarification would be much appreciated. Thank you.

@asobirov
Copy link
Contributor

Hey @felix-mittermeier, just gave it a run again and it seems to be working. Note that you will now need to use it as

sendGAEvent('event', 'buttonClicked', { value: 'xyz' }) instead of sendGAEvent({ event: 'buttonClicked', value: 'xyz' })

as mentioned in #62065 (comment)

@felix-mittermeier
Copy link

That was indeed the issue. Thanks a lot for the help ☺️
Guess I overlooked that the syntax has been changed. Now it's working as intended

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants