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

Fix sendGAEvent function params and type clearly #63226

Open
wants to merge 9 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ export function EventButton() {
return (
<div>
<button
onClick={() => sendGAEvent({ event: 'buttonClicked', value: 'xyz' })}
onClick={() => sendGAEvent('event-name', {
value: 'xyz'
})}
>
Send Event
</button>
Expand All @@ -314,7 +316,9 @@ export function EventButton() {
return (
<div>
<button
onClick={() => sendGAEvent({ event: 'buttonClicked', value: 'xyz' })}
onClick={() => sendGAEvent('event-name', {
value: 'xyz'
})}
>
Send Event
</button>
Expand Down
23 changes: 23 additions & 0 deletions packages/third-parties/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,27 @@ export default function Page() {
}
```

### Google Analytics Event

The `sendGAEvent` function can be used to send an event to Google Analytics. This function uses the [gtag.js](https://developers.google.com/analytics/devguides/collection/ga4/events) library under the hood.

```js
import { sendGAEvent } from '@next/third-parties/google'

export default function Page() {
return (
<button
onClick={() => {
// Send an event to Google Analytics
sendGAEvent('event-name', {
value: xyz,
})
}}
>
Click me
</button>
)
}
```

To get a better idea of how these components work, take a look at this [demo](https://test-next-script-housseindjirdeh.vercel.app/). <!--- TODO: Replace with a better demo page -->
4 changes: 2 additions & 2 deletions packages/third-parties/src/google/ga.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export function GoogleAnalytics(props: GAParams) {
)
}

export function sendGAEvent(..._args: Object[]) {
export function sendGAEvent(eventName: string, eventParameters: {[key: string]: string | number}) {
Copy link
Contributor

@jamesmillerburgess jamesmillerburgess Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this type is too restrictive.

Some gtag events take an items parameter, which is an array of Item objects.

https://developers.google.com/tag-platform/gtagjs/reference/events

Copy link
Contributor Author

@hjick hjick Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review!

The url you attached return 500 status. Is that url about gtag ```event``` command?

I can't find that items parameters in recent docs about gtag.
https://developers.google.com/tag-platform/gtagjs/reference#event

If the items parameter you're talking about is a gtag command, how about creating a function for each command separately?

Copy link
Contributor Author

@hjick hjick Apr 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added type GA events include items parameters you referenced.
If the event name entered by the user is the same as the event name in the link you referenced, it also provides autocomplete event parameters.

Can you check this changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your types won't allow custom events. Maybe there is an existing official set of types from Google that we could use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type allow custom events. And I also added types from Google docs.

And if user input types from Google official types, user can get autocomplete Google official params.

Also user can input custom types.
I pushed new commit. Please check that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your maintaining. I know you're busy, but could you please check one more time, this commit would be very helpful.

if (currDataLayerName === undefined) {
console.warn(`@next/third-parties: GA has not been initialized`)
return
}

if (window[currDataLayerName]) {
window[currDataLayerName].push(arguments)
window[currDataLayerName].push({0: 'event', 1: eventName, 2: eventParameters})
} else {
console.warn(
`@next/third-parties: GA dataLayer ${currDataLayerName} does not exist`
Expand Down