PHPackages                             ideasonpurpose/wp-google-analytics - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ideasonpurpose/wp-google-analytics

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ideasonpurpose/wp-google-analytics
==================================

Adds a wp\_head action to inject Google analytics code snippets

v1.1.2(2y ago)0121↓100%[1 issues](https://github.com/ideasonpurpose/wp-google-analytics/issues)[4 PRs](https://github.com/ideasonpurpose/wp-google-analytics/pulls)MITPHPCI passing

Since Mar 13Pushed 1y ago2 watchersCompare

[ Source](https://github.com/ideasonpurpose/wp-google-analytics)[ Packagist](https://packagist.org/packages/ideasonpurpose/wp-google-analytics)[ RSS](/packages/ideasonpurpose-wp-google-analytics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (18)Used By (0)

WordPress Google Analytics Library
==================================

[](#wordpress-google-analytics-library)

#### Version: 1.1.2

[](#version-112)

[![Packagist](https://camo.githubusercontent.com/e4d4b36550f51ca55f86f84f029ab5143eaadd51220b2f08dd708efff088ffd3/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f69646561736f6e707572706f73652f77702d676f6f676c652d616e616c7974696373)](https://packagist.org/packages/ideasonpurpose/wp-google-analytics)[![codecov](https://camo.githubusercontent.com/c072c92c86c4b7c414b46315c57207e6d44336757e4ed2b76066d9e16d858e49/68747470733a2f2f636f6465636f762e696f2f67682f69646561736f6e707572706f73652f77702d676f6f676c652d616e616c79746963732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/ideasonpurpose/wp-google-analytics)[![Coverage Status](https://camo.githubusercontent.com/b4f3d738cc01e2edeb5d0e4750e50e5f490c0c674bd462359ba0e1ac4b94b4aa/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f69646561736f6e707572706f73652f77702d676f6f676c652d616e616c79746963732f62616467652e737667)](https://coveralls.io/github/ideasonpurpose/wp-google-analytics)[![Code Climate maintainability](https://camo.githubusercontent.com/66dfd480d7926b21dc0c1d4e0dcec744f6f05573eba7bc3deca72789610a48cd/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f69646561736f6e707572706f73652f77702d676f6f676c652d616e616c7974696373)](https://codeclimate.com/github/ideasonpurpose/wp-google-analytics)[![styled with prettier](https://camo.githubusercontent.com/7c31269a4fab27bd4b327813e7a459cf7a76da0b0b009254bf949a20889d2b6c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c65645f776974682d70726574746965722d6666363962342e737667)](https://github.com/prettier/prettier)

This package adds Google Tag Manager to WordPress sites. The goal of the project is to help remove development noise from collected stats. Tracking snippets are only injected when no users are logged in, for development tasks where `WP_DEBUG` set to true, a fallback `UA-xxxx` tracking ID will be used.

Google Tag Manager code will only be injected when `is_user_logged_in()` is false. We want to collect traffic from visitors, not authors or developers. For sites with lower traffic, this can make a real difference.

When `is_user_logged_in()` is false, tracking code will be injected with either the primary or fallback tracking ID. This package assumes that when `WP_DEBUG` is true, the site is in development and should use a fallback tracking ID. Otherwise, with no one logged in and `WP_DEBUG` unset or false, the primary tracking id will be served.

Usage
-----

[](#usage)

This library is available on [Packagist](https://packagist.org/packages/ideasonpurpose/wp-google-analytics), just require it in **composer.json** to add it to the project or tell Composer to load the package:

```
$ composer require ideasonpurpose/wp-google-analytics

```

Then initialize the code with a primary and fallback tracking ID:

```
use IdeasOnPurpose\WP\GoogleAnalytics;

new GoogleAnalytics("G-XYZ456", "G-JKL890");
```

For the sake of future maintenance, it's a good idea to store tracking IDs in descriptive variables:

```
$client_prod_id = "G-XYZ456";
$local_dev_id = "G-JKL890";
new GoogleAnalytics($client_prod_id, $local_dev_id);
```

### Backwards compatibility with GA4 and Universal Analytics

[](#backwards-compatibility-with-ga4-and-universal-analytics)

In some situations, Google recommends [injecting two snippets](https://support.google.com/analytics/answer/9744165) into the page, one for the older Universal Analytics property, and then a copy with the new GA4 property ID.

Analytics ID arguments can be a single string or an array of strings. When multiple IDs are provided, a snippet will be injected for each:

```
new GoogleAnalytics(["UA-012345-1", "G-XYZ456"], $local_dev_id);
```

Will produce something like this:

```

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-012345-1');

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XYZ456');

```

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~213 days

Recently: every ~154 days

Total

12

Last Release

994d ago

Major Versions

v0.4.1 → v1.0.02021-12-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/fe11fb22cbf2972be8b48dde08f27fff4b6d7008ad682e8b42a0e0c57b0e34f2?d=identicon)[joemaller](/maintainers/joemaller)

---

Top Contributors

[![joemaller](https://avatars.githubusercontent.com/u/8320?v=4)](https://github.com/joemaller "joemaller (61 commits)")

---

Tags

pluginwordpressgoogleanalyticsgoogle-analyticsIdeas On Purpose

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ideasonpurpose-wp-google-analytics/health.svg)

```
[![Health](https://phpackages.com/badges/ideasonpurpose-wp-google-analytics/health.svg)](https://phpackages.com/packages/ideasonpurpose-wp-google-analytics)
```

###  Alternatives

[afragen/git-updater

A plugin to automatically update GitHub, Bitbucket, GitLab, or Gitea hosted plugins, themes, and language packs.

3.3k1.6k](/packages/afragen-git-updater)[appsero/client

Appsero Client

25431.7k8](/packages/appsero-client)[webdevstudios/cmb2-attached-posts

Custom field for CMB2 for creating post relationships.

13565.5k](/packages/webdevstudios-cmb2-attached-posts)[iceicetimmy/acf-post-type-selector

Post type selector for Advanced Custom Fields.

559.0k](/packages/iceicetimmy-acf-post-type-selector)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
