GithubHelp home page GithubHelp logo

wearerequired / required-google-analytics Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 0.0 190 KB

A lightweight WordPress plugin to add Google's analytics.js to your site, the modern way.

License: GNU General Public License v2.0

PHP 83.84% JavaScript 16.16%
wordpress wordpress-plugin google-analytics gtag

required-google-analytics's Introduction

Google Analytics

A lightweight WordPress plugin to add Google's global site tag (gtag.js) to your site, the modern way.

The modern way? The script is added with support for preloading, which will provide a small performance boost on modern browsers.
Modern browsers are those that support the async script attribute. This does not include IE 9 and older mobile browsers.

You can use this plugin with Universal Analytics or Google Analytics 4 properties or, to help ease the transition period, dual tagging is also supported.

Beside that, the plugin doesn't do more. No fancy report view in your admin, no authentication requirements, no additional scripts, and no ads for random blog posts.

Installation

Install via Composer

$ composer require wearerequired/required-google-analytics

Add you property ID at Settings > Reading > Google Analytics.

Disable Analytics for opted-out users

The plugin provides a function to let users opt out of Google Analytics measurement. If the function window.requiredGADoOptOut() is called a ga-opted-out item is stored in the local browser storage and the window property window['ga-disable-PROPERTY_ID'] will be set to true.

Example:

<a href="#" onclick="requiredGADoOptOut();return false">Opt-out from Google Analytics for this site.</a>

Event Tracking for Universal Analytics

The plugin provides a small layer that transform data attributes on an HTML element to a Google Analytics event. To enable the layer use the following filter:

add_filter( 'required_ga.enable_event_tracking', '__return_true' );

To track the click event of a button you can now use the following data attributes:

  • data-ga-event-action: Event action.
  • data-ga-event-on: Optional. Event trigger. Defaults to click and can be any DOM event.
  • data-ga-event-category: Optional. Event category.
  • data-ga-event-label: Optional. Event label.
  • data-ga-event-value: Optional. Event value.

Example usage:

<button data-ga-event-action="xyz">Click me!</button>

<button data-ga-event-action="play_video" data-ga-event-category="engagement">Click me!</button>

Additional Config Info

To configure custom dimensions and metrics you can use the required_ga.additional_config_info filter. By default the forceSSL and anonymizeIp fields are set to true.

Example usage:

add_filter( 'required_ga.enable_event_tracking', function( $config ) {
	$config['custom_map'] = [
		'dimension1' => 'author',
	];

	if ( is_singular( 'post' ) ) {
		$config['author'] = get_the_author_meta( 'display_name', get_post()->post_author )
	}

	return $config;
} );

Support for analytics.js (Universal Analytics)

Starting with version 2.0 the plugin enqueues gtag.js. If you need support for analytics.js you can continue using the 1.x branch.

$ composer require wearerequired/required-google-analytics:"^1.0"

required-google-analytics's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

required-google-analytics's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.