php 在网站上显示谷歌分析的访问者数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19092193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display number of visitors of google analytics on website
提问by Daniel Ramirez-Escudero
A client asked me to display the number of hits/visitors in their website. I would like to know how can you do display this.
一位客户让我在他们的网站上显示点击次数/访问者数量。我想知道你怎么能显示这个。
I would insert it in my footer as displayed:
我会将它插入到我的页脚中,如下所示:
If not possible with google analytics, do you know of a snippet which will work? I've checked websites which offer their services but they recollect information and I would like to learn to do it myself or with google analytics. My files are PHP so maybe there is something I can do with that?
如果谷歌分析不可能,你知道一个可以工作的片段吗?我检查过提供服务的网站,但他们会收集信息,我想自己或使用谷歌分析来学习。我的文件是 PHP,所以也许我可以用它做些什么?
回答by tony m
You can use google anlaytics api, which can be enabled in your google api console. For knowing the number of visitors in given time period, you can utilize Core Reporting APIand for knowing the current number of visitors in real time , you can use Realtime Reporting API
您可以使用google anlaytics api,它可以在您的 google api 控制台中启用。要了解给定时间段内的访问者数量,您可以使用Core Reporting API;要实时了解当前访问者的数量,您可以使用Realtime Reporting API
回答by Somnath Muluk
You can do graphical representation by using http://www.seethestats.com/
also.
Different type of counts you can get like Visits, Unique Visitors, Visits by Page title, etc
您也可以使用图形表示http://www.seethestats.com/
。您可以获得不同类型的计数,例如访问次数、唯一身份访问者、按页面标题的访问次数等
- Create account on http://www.seethestats.com/.
- Select GA stats you want to publish.
- Insert stats widget on your website.
- 在http://www.seethestats.com/上创建帐户。
- 选择您要发布的 GA 统计信息。
- 在您的网站上插入统计小部件。
回答by MReza
This APIs (Management API Client Libraries & Sample Code) help you easily and quickly.
此 API(管理 API 客户端库和示例代码)可帮助您轻松快速地进行。
回答by Petr Havlik
Unless you get whitelisted for Realtime Reporting API, there is no way to get current number of online visitor from GA. And if with the Realtime API, the implementation might be tricky and require a bit of coding as well.
除非您被列入Realtime Reporting API 的白名单,否则无法从 GA 获取当前在线访问者的数量。如果使用实时 API,实现可能会很棘手,还需要一些编码。
The easiest way to go is to use tools like StatCounter. The numbers won't probably align (there are no two web analytics tools that would give you the same numbers anyway :-), but they will be "accurate enough" and most importantly - you will be done with the implementation part in no time!
最简单的方法是使用StatCounter 之类的工具。这些数字可能不会对齐(无论如何,没有两个网络分析工具可以为您提供相同的数字:-),但它们将“足够准确”,最重要的是 - 您将立即完成实施部分!
回答by Daniel Ramirez-Escudero
I found a solution once I investigated again:
我再次调查后找到了解决方案:
<?php
session_start();
$counter_name = "counter.txt";
// Check if a text file exists. If not create one and initialize it to zero.
if (!file_exists($counter_name)) {
$f = fopen($counter_name, "w");
fwrite($f,"0");
fclose($f);
}
// Read the current value of our counter file
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
// Has visitor been counted in this session?
// If not, increase counter value by one
if(!isset($_SESSION['hasVisited'])){
$_SESSION['hasVisited']="yes";
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
}
echo "You are visitor number $counterVal to this site";
This snippet can be found clicking here. THe credits are for him. I display it to see if this can help somebody else in this topic.
可以点击这里找到这个片段。学分是给他的。我展示它是为了看看这是否可以帮助这个主题的其他人。