Add one or more Google Analytics trackers to your website.
Minimalistic Google Analytics plugin that lets you add one or more trackers and control exactly how the script is rendered.
This plugin lets you control wether the Google Analytics script is printed in the <head>
-element, the <body>
-element or using a custom action.
There’s also a filter available, if you want to completely override the code that prints the analytics code. This way you can use the plugin simply for storing the property ID’s in the database and manually render the analytics script exactly as your want it.
The filter for overriding the script code is ahr-google-analtyics/script_file_path
and you’d use it like so:
add_filter('ahr-google-analtyics/script_file_path', function($default_path){ // return an absolute file path to the file you want to use for rendering the script }, 10, 1);
If you need to manipulate the Google Analytics property ids before they are printed, then use the ahr-google-analtyics'/property_ids
filter. It’ll pass you an array of property ids as its single argument.
If you need to pull the ids from the database, in any other context, you can use $property_ids = get_option( AhrGoogleAnalytics::OPTION_IDS );
This plugin will always give you an array of ids. Even if you just have one.
Please feel free to contribute improvements, report bugs or suggest new features via the GitHub repo
ahr-google-analytics
folder to the /wp-content/plugins/
directory OR search for ‘Multiple Google Analytics’ and add the plugin using WordPress’ plugin browser.ahr-google-analytics
folder to the /wp-content/plugins/
directory OR search for ‘Multiple Google Analytics’ and add the plugin using WordPress’ plugin browser.It’s the string looking like UA-XXXXXXX-X
next to your website URL on https://analytics.google.com/analytics/web/
If you’re building a JavaScript-based web app and manually need to track custom routes or events you can use the ga.getAll()
method to get an array of registered trackers and then run the tracking logic for each tracker.
For example, this will track page views based on custom routes:
if ( typeof ga === 'function' && typeof ga.getAll === 'function' ) { ga.getAll().forEach(tracker => { tracker.set('page', *my-custom-route*); // Custom route e.g. '/my-page' tracker.send('pageview'); }) }
And here’s an example of tracking custom events:
if ( typeof ga === 'function' && typeof ga.getAll === 'function' ) { ga.getAll().forEach(tracker => { tracker.send('event', { eventCategory: 'Search', eventAction: 'Query', eventLabel: *query*, // Your search query eventValue: *matches.length* // Optionally, send the number of matches for the query }); }) }
Release Date: September 11th, 2017
Add support for the new longer google analytics ID’s that would previously be rejected by the validator
Release Date: March 15th, 2017
Fixes a bug where default values weren’t set properly when first activating plugin
Release Date: December 11th, 2016
Fixes typos in plugin activation/deactivation callback functions
Release Date: November 2nd, 2016