Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Add options for Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t committed Mar 15, 2015
1 parent 49fca89 commit 05fca06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,27 @@ add_theme_support('soil-nice-search');
Enable HTML5 Boilerplate's Google Analytics snippet

```php
add_theme_support('soil-google-analytics');
define('GOOGLE_ANALYTICS_ID', 'UA-XXXXXX');
add_theme_support('soil-google-analytics', 'UA-XXXXX-Y');
```

By default, the GA snippet will only be shown for non-administrators. Administrators will get a dummy `ga()` function that writes its arguments to the console log for testing and development purposes. If you define `WP_ENV`, then non-production environments will always get dummy `ga()` function. You can override this via the `soil/displayGA` filter.
By default, the script will be loaded from the `wp_footer` hook, but you can specify any other action hook as the third parameter.

```php
add_filter('soil/displayGA', '__return_true'); // Appends H5BP's GA snippet
add_filter('soil/displayGA', '__return_false'); // Appends a dummy `ga()` function that writes arguments to console log
add_theme_support('soil-google-analytics', 'UA-XXXXX-Y', 'wp_head'); // script will load during wp_head
```

#### Dummy `ga()` Function

This module will load a dummy `ga()` function for non-administrators as well as in non-`production` environments (when `WP_ENV` is defined). The function takes all arguments passed to it and logs them to the JavaScript console.

You can override whether the dummy function is displayed via the `soil/dummyGA` filter.

```php
add_filter('soil/dummyGA', '__return_true'); // Appends a dummy `ga()` function
add_filter('soil/dummyGA', '__return_false'); // Appends H5BP's GA snippet
```


### JS to Footer

Move all scripts to `wp_footer` action hook with:
Expand Down
33 changes: 16 additions & 17 deletions modules/google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
*
* Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
* You can enable/disable this feature in functions.php (or lib/config.php if you're using Sage):
* add_theme_support('soil-google-analytics');
* define('GOOGLE_ANALYTICS_ID', 'UA-XXXXXX');
* add_theme_support('soil-google-analytics', 'UA-XXXXX-Y', 'wp_footer');
*/
function google_analytics() {
if (!defined('GOOGLE_ANALYTICS_ID') || !GOOGLE_ANALYTICS_ID) {
list($ga_id, $hook) = $options + ['','wp_footer'];
add_action($hook, function () use ($ga_id) {
if (!$ga_id) {
return;
}
$displayGA = (!defined('WP_ENV') || WP_ENV === 'production') && !current_user_can('manage_options');
$dummyGA = (defined('WP_ENV') && WP_ENV !== 'production') || current_user_can('manage_options');
?>
<script>
<?php if (apply_filters('soil/displayGA', $displayGA)) : ?>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php else : ?>
<script>
<?php if (apply_filters('soil/dummyGA', $dummyGA)) : ?>
function ga() {if (window.console) {console.log('Google Analytics: ' + [].slice.call(arguments));}}
<?php else : ?>
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php endif; ?>
ga('create','<?= GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
ga('create','<?= $ga_id; ?>','auto');ga('send','pageview');
</script>
<?php
}
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
<?php
}, 20);

0 comments on commit 05fca06

Please sign in to comment.