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

Commit

Permalink
Add H5BP GA snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t committed Mar 13, 2015
1 parent f122c75 commit b114433
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -56,6 +56,15 @@ Enable Soil's nice search (`/search/query/`) with:
add_theme_support('soil-nice-search');
```

### Google Analytics

Enable HTML5 Boilerplate's Google Analytics snippet

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

This comment has been minimized.

Copy link
@retlehs

retlehs Mar 13, 2015

Member

add space after comma

```

### JS to Footer

Move all scripts to `wp_footer` action hook with:
Expand Down
36 changes: 36 additions & 0 deletions modules/google-analytics.php
@@ -0,0 +1,36 @@
<?php

namespace Roots\Soil\GoogleAnalytics;

/**
* Google Analytics snippet from HTML5 Boilerplate
*
* 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');

This comment has been minimized.

Copy link
@retlehs

retlehs Mar 13, 2015

Member

add space after comma

*/
function google_analytics() {
$logToConsole = (defined('WP_ENV') && WP_ENV !== 'production') || current_user_can('manage_options');
?>
<script>
<?php if ($logToConsole) : ?>
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');

This comment has been minimized.

Copy link
@kalenjohnson

kalenjohnson Mar 13, 2015

Contributor

Both Sage and Soil will be using short opening tags moving forward, as part of the PHP 5.4 minimum.

</script>
<?php
}

// This is in 'init' action to give user time to define GOOGLE_ANALYTICS_ID
add_action('init', function () {

This comment has been minimized.

Copy link
@retlehs

retlehs Mar 13, 2015

Member

remove space before (

This comment has been minimized.

Copy link
@QWp6t

QWp6t Mar 13, 2015

Author Member

That's how I had it before, but Travis yelled at me. :(

This comment has been minimized.

Copy link
@kalenjohnson

kalenjohnson Mar 13, 2015

Contributor

Travis will yell at you no matter what

if (defined('GOOGLE_ANALYTICS_ID')) {
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20);
}
}, 20);

9 comments on commit b114433

@QWp6t
Copy link
Member Author

@QWp6t QWp6t commented on b114433 Mar 13, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corvannoorloos

You are more than welcome to remove it yourself in your theme.

remove_action('wp_footer', 'Roots\\Soil\\GoogleAnalytics\\google_analytics', 20);
add_action('wp_head', 'Roots\\Soil\\GoogleAnalytics\\google_analytics', 20);

@austinpray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corvannoorloos agree, document header is best

@kalenjohnson
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposal: Change the namespace from GoogleAnalytics, less specific and open to more code snippets in the future. Could possibly reduce the function name down to google() as well.

@QWp6t
Copy link
Member Author

@QWp6t QWp6t commented on b114433 Mar 13, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. Just change namespace once other snippets are added. No biggie. Right now namepsace matches Soil module's name for each module. This namespace simply follows that convention.

I'm against adding this to document header. It serves no benefit. I only want hits to be counted if the visitor actually loaded the whole page, so having this in the footer makes more sense. This is the same reasoning used by H5BP. Every time someone brings it up, it gets shot down, and I believe their decision is unanimous, i.e., I have not seen anyone in the h5bp team argue for having it in the head. I'm in favor of following their lead.

@austinpray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same reasoning used by H5BP.

What does H5BP's have to do with anything? The vendor recommends putting this thing in the header to work properly.

@retlehs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does H5BP's have to do with anything?

...it's h5bp's GA snippet. it's staying in the footer for a reason (and has been discussed on the h5bp repo many times)

@austinpray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely should be configurable then.

@QWp6t
Copy link
Member Author

@QWp6t QWp6t commented on b114433 Mar 13, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is configurable.

remove_action('wp_footer', 'Roots\\Soil\\GoogleAnalytics\\google_analytics', 20);
add_action('wp_head', 'Roots\\Soil\\GoogleAnalytics\\google_analytics', 20);

tada! :D

@austinpray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@QWp6t my man

Gonna PR dat into the dox

Please sign in to comment.