Extends Google Analytics 4 by including custom data for enhanced GA4 analytics.
- User Properties: Sets
user_properties
withis_subscriber
(0 or 1) on all pages. - Post Data: On single post pages, outputs
post_author
,post_category
, andpost_tags
to thewindow.dataLayer
.
Example code output:
<script id="ga4-ext-gtagjs-js-before">
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("set", "linker", {
"domains": ["example.com"]
});
gtag("js", new Date());
gtag("config", "G-1234", {
"post_author": "dev",
"post_category": "news",
"post_tags": "featured"
});
gtag("set", "user_properties", {
is_subscriber: 0
});
</script>
<script src="https://www.googletagmanager.com/gtag/js?id=G-1234" id="ga4-ext-gtagjs-js" defer data-wp-strategy="defer"></script>
To enable GA4 tracking, follow these steps:
- In your WordPress dashboard, go to Settings > General.
- Input your GA4 ID (e.g.,
G-1234567
) into the Google Analytics 4 ID field. - Click Save Changes.
To use the custom data in Google Analytics 4, you need to create custom events and parameters in the GA4 dashboard. Here's how you can set it up:
- Navigate to Google Analytics and sign in with your account credentials with an Administrative role.
- Select the property you want to track.
- Access the Admin Panel - Click the Admin gear icon located in the bottom-left corner of the interface.
- In the Data display section, click on
Custom definitions
. - For each custom dimension (post_author, post_category, post_tags), follow these steps: a. Click Create Custom Dimension. b. Dimension Name: Post XXX (e.g., Post Author) c. Scope: Event d. Event Parameter: post_author e. Click Save.
- WordPress: Version 6.5 or higher.
- PHP: Version 8.1 or higher.
This plugin is licensed under the GPLv3 or later.