使用PHP检索Google Analytics(分析)访问和PageViews

Google Analytics is an outstanding website analytics tool that gives you way more information about your website than you probably need. Better to get more than you want than not enough, right? Anyways I check my website statistics more often than I should and it ends up taking a few minutes to get logged in, select the right site, select the current day, etc. I found a great Google Analytics PHP API that allows me to get just the statistics I'm looking for.

Google Analytics(分析)是一款出色的网站分析工具,可为您提供有关网站的更多信息,而您可能不需要这些信息。 多于获得想要的总比没有得到的多,对吧? 无论如何,我都会比我更频繁地查看我的网站统计信息,最终要花几分钟才能登录,选择合适的网站,选择当前日期等。我找到了一个很棒的Google Analytics(分析)PHP API,它使我能够我正在寻找的统计数据。

PHP库 (The PHP Library)

The PHP class I found, analytics, can be downloaded at http://www.swis.nl/ga/. The site also gives a few solid examples.

我发现PHP类Analytics(分析 )可以从http://www.swis.nl/ga/下载。 该站点还提供了一些可靠的示例。

PHP (The PHP)


//session_start for caching, if desired
session_start();
//get the class
require 'ga/analytics.class.php';
//sign in and grab profile
$analytics = new analytics('david@davidwalsh.name', 'myP@ssw0rd');
$analytics->setProfileByName('davidwalsh.name');
//set the date range for which I want stats for (could also be $analytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'))
$analytics->setMonth(date('n'), date('Y'));
//get array of visitors by day
print_r($analytics->getVisitors());
//get array of pageviews by day
print_r($analytics->getPageviews());


As you'd expect, we first grab the class and immediately sign providing your credentials and website profile (you can just use your domain). Once authenticated we set a date range and retrieve our visitors and pageviews.

正如您所期望的,我们首先参加该课程,然后立即签名提供您的凭据和网站配置文件(您可以使用您的域)。 通过身份验证后,我们将设置日期范围并检索访问者和综合浏览量。

PHP示例结果 (The Sample PHP Results)

The above code retrieved visits and pageviews for the current month. What's returned is an array that looks as follows:

上面的代码检索了当月的访问量和综合浏览量。 返回的是一个如下所示的数组:


Array
(
    [01] => 6539
    [02] => 6677
    [03] => 6160
    [04] => 5563
    [05] => 2964
    [06] => 2973
    [07] => 5080
    [08] => 6078
    [09] => 5927
    [10] => 6177
	...
)


A very simple array numbered by day. You could do anything you wanted with the array -- create averages, peaks, lows, etc.

一个非常简单的数组,按天编号。 您可以对阵列进行任何所需的操作-创建平均值,峰值,低点等。

高级用法 (Advanced Usage)

The analytics class also allows you to retrieve search keywords and anything else you might like:

Analytics类还允许您检索搜索关键字以及您可能喜欢的其他任何东西:


$keywords = $analytics->getData(array(
						'dimensions' => 'ga:keyword',
		                'metrics' => 'ga:visits',
		                'sort' => 'ga:keyword'
						)
					);


The PHP analytics class is a masterpiece. Rest assured I'll be doing a ton of work with this class for customers and my own websites! Do you have any ideas as to what you could use this class for? Share!

PHP 分析类是杰作。 请放心,我将在此类课程上为客户和自己的网站做大量工作! 您对使用该课程有什么想法? 分享!

翻译自: https://davidwalsh.name/php-google-analytics

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值