Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a little PHP Class that add google analytics to WordPress without lowering the Pagespeed Index. It saves analytics.js on the server and updates it daily via wp-cron.
<?php
namespace sayhello\Theme;
/**
* Get Google Analytics.js and saves it locally, inserts tracking code into wp_footer
* Advantage: No 2h googleanalytics.js cache
* +1 Google Page Speed Point
*
* @since 0.0.1
*
*/
class GoogleAnalytics {
/**
* can either be a PropertyID, an option Key or an ACF Field
* @var string
*/
public static $property_id = '';
/**
* wp_upload_dir()['basedir']
* @var string
*/
public static $dir = '';
/**
* wp_upload_dir()['baseurl'];
* @var string
*/
public static $url = '';
public function __construct() {
$this->property_id = 'none';
$this->dir = wp_upload_dir()['basedir'];
$this->url = wp_upload_dir()['baseurl'];
}
/**
* This Functions sets the Google Analytics Property ID
* @param $id can either be a PropertyID, an option Key or an ACF Field
*/
public function set_property_id( $id ) {
$this->property_id = $id;
}
/**
* runs filters an actions for the instance
* @return none
*/
public function run() {
add_action( 'init', [ $this, 'check_if_exists' ] );
if ( ! wp_next_scheduled( 'sheduled_GAProxy' ) ) {
wp_schedule_event( ( new \DateTime() ) -> setTime( 1, 0 ) -> getTimestamp(), 'daily', 'sheduled_GAProxy' );
}
add_action( 'sheduled_GAProxy', [ $this, 'update_file' ] );
add_action( 'wp_footer', [ $this, 'set_analytics_code' ] );
}
/**
* saves the analytics.js file if not already saved
* @return none
*/
public function check_if_exists() {
if ( ! file_exists( $this->dir . '/analytics.js' ) ) {
$this->update_file();
}
}
/**
* updates the analytics.js file and the check-option
* @return none
*/
public function update_file() {
update_option( 'analytics_regenerated', date( 'd-m-Y H:i' ) );
file_put_contents( $this->dir . '/analytics.js', fopen( 'https://www.google-analytics.com/analytics.js', 'r' ) );
}
/**
* echoes the analytics code
*/
public function set_analytics_code() {
$id = $this->property_id;
if ( $this->is_valid_property( $id ) ) {
$property_id = $id;
} elseif ( $this->is_valid_property( get_field( $id, 'option' ) ) ) {
$property_id = get_field( $id, 'option' );
} elseif ( $this->is_valid_property( get_option( $id ) ) ) {
$property_id = get_option( $id );
} else {
if ( is_user_logged_in() ) {
?><!-- Google Analytics: invalid Property ID --><?php
}
return;
}
if ( is_user_logged_in() ) {
?><!-- Google Analytics: Property ID <?php echo $property_id; ?>, Script regenerated <?php echo get_option( 'analytics_regenerated' ); ?> --><?php
}
?><script id="GoogleAnalytics">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','<?php echo $this->url . '/analytics.js'; ?>','ga');
ga('create', '<?php echo $property_id; ?>', 'auto');
ga('send', 'pageview');
</script><?php
}
/**
* Helpers
*/
/**
* checks if given string is a vaild google analytics propery ID
* @param string $id property ID
* @return boolean
*/
public function is_valid_property( $id ) {
return preg_match( '/^ua-\d{4,9}-\d{1,4}$/i', strval( $id ) );
}
}
$prefix_ga = new GoogleAnalytics();
$prefix_ga_property_id = ''; //could be a property ID, a wp-options key, or a ACF field key
$prefix_ga->set_property_id( $prefix_ga_property_id );
$prefix_ga->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.