Google Tracking Optimization saves a copy of supported Google tracking scripts on your own server and loads that copy instead of the file on Google’s servers. The browser then needs no extra connection to Google just to download the script, and the file is served with your own server’s caching rules. Speed tests such as PageSpeed Insights often flag these third-party scripts.
Available in: Powered Cache Premium
Where to find it: Powered Cache → Integrations
How it works #
- When WordPress builds a front-end page, Powered Cache looks through its
<script>tags, both script files and inline code, for a JavaScript address ongoogletagmanager.comorgoogle-analytics.com. - It downloads that file and saves it in
wp-content/cache/externaljs/. - It replaces Google’s address in the page with the address of the local copy. A
?t=parameter with the file’s save time is added, so browsers pick up a refreshed copy.
If the download fails and no local copy exists yet, the page keeps Google’s original address, so tracking keeps working. Admin screens, AJAX and REST API requests, and feeds are not changed.
How the copy stays up to date #
There is no separate schedule. When WordPress builds a page and the local copy is older than 24 hours, Powered Cache downloads the script again and overwrites the copy. That page waits for the download, so one page build a day can be a little slower. Pages served from the page cache are not delayed. If the new download fails, the existing copy stays in use.
Purge All Cache deletes the local copies, and the next page build downloads fresh ones.
Turn on Google Tracking Optimization #
- Go to Powered Cache → Integrations.
- Turn on Google Tracking Optimization (Optimize Google tracking scripts).
- Click Save Settings, then run Purge All Cache so cached pages are rebuilt with the local address.
- Open a page in a private browser window and view its source. The Google tag (
gtag/js) oranalytics.jsaddress should now point to/wp-content/cache/externaljs/. Google Tag Manager snippets keep loading from Google. See What is and is not hosted locally. - Open the Realtime report in Google Analytics and confirm that your visit is counted.
Settings #
Google Tracking Optimization #
Hosts supported Google tracking scripts on your server. Default: off. There are no other options.
Changed in 4.0: this setting was called Google Tracking in Powered Cache 3.x. Only the name changed, and your saved choice was kept.
What is and is not hosted locally #
Powered Cache downloads the address only up to js. Anything after it, such as ?id=G-XXXXXXX or ?id=GTM-XXXXXXX, is not part of the download. This affects the common snippets differently:
| Snippet | Result |
|---|---|
Google Analytics analytics.js | Hosted locally. |
Google tag gtag/js?id=G-… | Powered Cache downloads the general gtag/js file without your measurement ID and keeps ?id=… on the local address. Check the Realtime report after you turn this on. If visits are missing, turn the setting off. |
Google Tag Manager gtm.js?id=GTM-… | Google does not return a container without its ID, so the download fails and the container keeps loading from Google. |
Scripts that Google’s own code loads later, after the page has started, always come from Google.
Privacy notes #
- Hosting the script file locally does not stop tracking. Once it runs, the script sends data to Google as before. You still need the same consent and privacy notices.
- Your server contacts Google about once a day to refresh the copy. That request does not contain visitor data.
- If your cookie consent tool blocks scripts by their Google address, the address changes to your own domain. Test that the tool still blocks the script until the visitor agrees. See cookie consent plugins.
For developers #
The powered_cache_google_tracking_ttl filter changes the refresh age in seconds (default one day). The powered_cache_tracking_cache_path filter changes the storage folder. See the Hooks Reference.