Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Sorry, something went wrong. |
||
*/ | ||
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.
Sorry, something went wrong.
kalenjohnson
Contributor
|
||
</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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
if (defined('GOOGLE_ANALYTICS_ID')) { | ||
add_action('wp_footer', __NAMESPACE__ . '\\google_analytics', 20); | ||
} | ||
}, 20); |
9 comments
on commit b114433
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
add space after comma