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

Add GTM-injected GA4 support #130

Merged
merged 6 commits into from
Aug 1, 2024
Merged

Add GTM-injected GA4 support #130

merged 6 commits into from
Aug 1, 2024

Conversation

mehigh
Copy link
Member

@mehigh mehigh commented Jul 28, 2024

Fixes #

Tasks

  • Setup a fallback to check for window.dataLayer.push and initialize gtag when gtag is unavailable

Describe the Approach

  • This covers the GA4 tracking setup through Google Tag Manager without exposing the gtag, by initializing it only once as per the configuration option.

Release Checklist

  • Review the release guidelines in the contribution documentation.
  • Update the plugin Version: header value in site-performance-tracker.php.
  • Confirm that automated tests are passing for this pull request.

@@ -76,6 +73,21 @@ export function sendToAnalytics( { name, value, delta, id, attribution, rating }
}

if ( analyticsData && analyticsData.ga4_id ) {
getDeliveryFunction( 'gtag' )( 'event', name, eventParams );
if ( typeof window.gtag !== 'function' && window.dataLayer && typeof window.dataLayer.push === 'function' ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Initialize gtag only once.

@mehigh mehigh requested a review from pereirinha July 28, 2024 22:29
js/src/send-to-analytics.js Outdated Show resolved Hide resolved
Copy link
Member

@pereirinha pereirinha left a comment

Choose a reason for hiding this comment

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

@mehigh

I did some tests and reading and I'm not finding any drawback with this approach. In all fairness gtag is just a wrapper of the dataLayer. ref

I removed an extra line to make the linter happy.

@pereirinha pereirinha merged commit 40e4e26 into master Aug 1, 2024
1 check passed
@pereirinha pereirinha deleted the feature/gtm branch August 1, 2024 09:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants