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
.
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 install the plugin via Composer, follow these steps:
-
Add the Repository:
-
Open your project's
composer.json
file. -
Add the following under the
repositories
section:"repositories": [ { "type": "vcs", "url": "https://github.com/xwp/ga4-extensions" } ]
-
-
Require the Plugin:
-
Run the following command in your terminal:
composer require xwp/ga4-extensions
-
-
Activate the Plugin:
- Once installed, activate the plugin through the 'Plugins' menu in WordPress.
-
Download the Plugin:
- Download the
ga4-extensions
plugin folder.
- Download the
-
Upload the Plugin:
- Add the
ga4-extensions
folder to the/wp-content/plugins/
directory of your WordPress installation.
- Add the
-
Activate the Plugin:
- Activate the plugin through the 'Plugins' menu in WordPress.
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 # 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.