php 将Google Analytics添加到页面

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 将Google Analytics添加到页面相关的知识,希望对你有一定的参考价值。

<script>
	console.log('Google Analytics is NOT tracking this page');
</script>
/*
Add this bit to header.php and it will only track the page if it is on your live site.
Then put the rest of the code in an "inc" directory (or wherever you want it).
The console log is a quick way to check if your live site pages are being tracked
*/
<?php

$server = $_SERVER['SERVER_NAME'];
If ( $server === "www.website.co.uk" ) :
	include_once("inc/analyticstracking.php");
else :
	include_once("inc/notracking.php");
endif;
<script>
	console.log('Google Analytics is tracking this page');

	(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','//www.google-analytics.com/analytics.js','ga');

	ga('create', 'UA-XXXXXXXX-1', 'auto');
	ga('send', 'pageview');

	var trackOutboundLink = function(url) {
		ga('send', 'event', 'outbound', 'click', url, {
			'transport': 'beacon',
			'hitCallback': function(){document.location = url;}
		});
	}

</script>

以上是关于php 将Google Analytics添加到页面的主要内容,如果未能解决你的问题,请参考以下文章

php 将Google Analytics跟踪代码添加到WordPress的简单插件

php 将Google Analytics跟踪代码添加到WordPress的简单插件

Google Analytics Reporting Api - PHP:添加用户帐户

php Wordpress functions.php片段使用Yoast的插件将Google Analytics跟踪添加到wp_nav_menu

php 使用Google Analytics自定义维度添加WordPress作者跟踪

php 将页面内容添加到页脚版权