[英]get unique visitors count with certain page ids - php
使用wordpress。 我已經收集了每日視圖(全部視圖和唯一視圖),現在想查看某些頁面的每月統計信息。 我可以獲得總觀看次數,但是在獲得唯一觀看次數時遇到問題。 我做了以下工作:
$ids = '181,57,123';
$certainPageIds = explode(',', $ids);
foreach($certainPageIds as $id){
$uniqueViews = $wpdb->get_results("SELECT distinct IP as all_uniques,
DATE_FORMAT( insertion_date, '%b' ) as month_name, DATE_FORMAT( insertion_date,
'%Y-%M' ) as full_date FROM daily_unique_views WHERE page_id = '".$id."'");
if($uniqueViews){
$uniqueViewsEncodedArray = json_decode(json_encode($uniqueViews), True);
var_dump($uniqueViewsEncodedArray);
}
}
上面的數組返回以下內容:
array(2) {
[0]=>
array(3) {
["all_uniques"]=>
string(13) "111.11.11.111"
["month_name"]=>
string(3) "Dec"
["full_date"]=>
string(13) "2016-December"
}
[1]=>
array(3) {
["all_uniques"]=>
string(13) "22.222.222.22"
["month_name"]=>
string(3) "Dec"
["full_date"]=>
string(13) "2016-December"
}
}
array(2) {
[0]=>
array(3) {
["all_uniques"]=>
string(13) "111.11.11.111"
["month_name"]=>
string(3) "Nov"
["full_date"]=>
string(13) "2016-November"
}
[1]=>
array(3) {
["all_uniques"]=>
string(13) "111.11.11.111"
["month_name"]=>
string(3) "Dec"
["full_date"]=>
string(13) "2016-December"
}
[2]=>
array(3) {
["all_uniques"]=>
string(12) "33.333.3.333"
["month_name"]=>
string(3) "Dec"
["full_date"]=>
string(13) "2016-December"
}
}
array(1) {
[0]=>
array(3) {
["all_uniques"]=>
string(13) "111.11.11.111"
["month_name"]=>
string(3) "Oct"
["full_date"]=>
string(13) "2016-October"
}
}
因此,它返回每個頁面的每月唯一視圖。 但是您會發現111.11.11.111
出現了很多次。 因此,每個頁面的12月我都有相同的IP。 但是我每個月只需要統計一次相同的IP。 我如何才能做到這一點? 與現有結果有關系嗎,或者應該用sql修復? 有什么想法嗎?
您可以使用cte(通用表expresson),如下所示:
DECLARE @ips TABLE(
id int,
ip varchar(3),
month varchar(3)
)
INSERT INTO @ips VALUES (1,'ip1', 'jan')
INSERT INTO @ips VALUES (2,'ip2', 'jan')
INSERT INTO @ips VALUES (3,'ip1', 'feb')
INSERT INTO @ips VALUES (4,'ip2', 'feb')
INSERT INTO @ips VALUES (5, 'ip2', 'jan');
-- Define the CTE expression name and column list.
WITH cte_ips (ip, month)
AS
-- Define the CTE query.
(
SELECT ip, month FROM @ips
)
-- count the results
SELECT COUNT(DISTINCT ip), COUNT(ip), month FROM Cte_ips GROUP BY Month
-- Define the outer query referencing the CTE name.
SELECT COUNT(DISTINCT IP) AS UniqueIPs, COUNT(IP) AS TotalIPs, month FROM cte_ips
GROUP BY month
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.