text 这是一个小的PHP类,可以在不降低Pagespeed索引的情况下将Google分析添加到WordPress。它将analytics.js保存在服务器上
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 这是一个小的PHP类,可以在不降低Pagespeed索引的情况下将Google分析添加到WordPress。它将analytics.js保存在服务器上相关的知识,希望对你有一定的参考价值。
<?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();
以上是关于text 这是一个小的PHP类,可以在不降低Pagespeed索引的情况下将Google分析添加到WordPress。它将analytics.js保存在服务器上的主要内容,如果未能解决你的问题,请参考以下文章
php 卸妆标签na pag do produto - Woocommerce
php get_section_param_from_detail_page.php
攻防世界(Web_php_include)
php设计六大原则
如何在不使用require_once的情况下自动加载和调用独立的PHP类?
php 字符串连接,性能