Display number of visitors of google analytics on website

为君一笑 提交于 2019-11-29 12:30:53

问题


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?


回答1:


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 API and for knowing the current number of visitors in real time , you can use Realtime Reporting API




回答2:


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

  1. Create account on http://www.seethestats.com/.
  2. Select GA stats you want to publish.
  3. Insert stats widget on your website.

ex. http://www.seethestats.com/site/monvidedressing.ch




回答3:


This APIs (Management API Client Libraries & Sample Code) help you easily and quickly.




回答4:


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.

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!




回答5:


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.



来源:https://stackoverflow.com/questions/19092193/display-number-of-visitors-of-google-analytics-on-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!