Created
June 23, 2019 13:33
-
-
Save nczz/db37d06ea4e8ca80168ad891a3df1440 to your computer and use it in GitHub Desktop.
[WordPress] 從 Google Analytics 匯入網站人氣的外掛組合技 https://www.mxp.tw/8529/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (function_exists('gapp_options')) { | |
function gapp_get_total_pageviews($format = true) { | |
$options = gapp_options(); | |
if (empty($options['gapp_token'])) { | |
//還沒授權過就先回傳預設值 | |
return $options['gapp_defaultval']; | |
} | |
//起始日 | |
$startDate = $options['gapp_startdate']; | |
//判斷有沒有設定過,沒有的話就設定卡一年(?) | |
$namespaceKey = get_transient('gapp-namespace-key'); | |
if ($namespaceKey === false) { | |
$namespaceKey = uniqid(); | |
set_transient('gapp-namespace-key', $namespaceKey, YEAR_IN_SECONDS); | |
} | |
$gaTransName = 'gapp-transient-total_pageviews'; | |
$gaTransName .= '-' . $namespaceKey; | |
$totalResult = get_transient($gaTransName); | |
if ($totalResult !== false && is_numeric($totalResult)) { | |
return ($format) ? number_format_i18n($totalResult) : $totalResult; | |
} else { | |
if (empty($options['gapp_token'])) { | |
return $options['gapp_defaultval']; | |
} | |
//先取得從起始日到今天的總瀏覽人數 | |
$gapp_metric = 'ga:pageviews'; | |
$json = gapp_api_call('https://www.googleapis.com/analytics/v3/data/ga', | |
array('ids' => 'ga:' . $options['gapp_wid'], | |
'start-date' => $startDate, | |
'end-date' => date('Y-m-d'), | |
'metrics' => $gapp_metric) | |
, false); | |
if (isset($json->totalsForAllResults->{$gapp_metric})) { | |
$totalResult = $json->totalsForAllResults->{$gapp_metric}; | |
set_transient($gaTransName, $totalResult, 60 * $options['gapp_cache']); | |
return ($format) ? number_format_i18n($totalResult) : $totalResult; | |
} else { | |
$default_value = $options['gapp_defaultval']; | |
set_transient($gaTransName, $default_value, 60 * $options['gapp_cache']); | |
return $options['gapp_defaultval']; | |
} | |
} | |
} | |
function gapp_get_today_pageviews($format = true) { | |
$options = gapp_options(); | |
if (empty($options['gapp_token'])) { | |
//還沒授權過就先回傳預設值 | |
return $options['gapp_defaultval']; | |
} | |
//起始日指定今天 | |
$startDate = date('Y-m-d'); | |
//判斷有沒有設定過,沒有的話就設定卡一年(?) | |
$namespaceKey = get_transient('gapp-namespace-key'); | |
if ($namespaceKey === false) { | |
$namespaceKey = uniqid(); | |
set_transient('gapp-namespace-key', $namespaceKey, YEAR_IN_SECONDS); | |
} | |
$gaTransName = 'gapp-transient-today_pageviews'; | |
$gaTransName .= '-' . $namespaceKey; | |
$totalResult = get_transient($gaTransName); | |
if ($totalResult !== false && is_numeric($totalResult)) { | |
return ($format) ? number_format_i18n($totalResult) : $totalResult; | |
} else { | |
if (empty($options['gapp_token'])) { | |
return $options['gapp_defaultval']; | |
} | |
//先取得從起始日到今天的總瀏覽人數 | |
$gapp_metric = 'ga:pageviews'; | |
$json = gapp_api_call('https://www.googleapis.com/analytics/v3/data/ga', | |
array('ids' => 'ga:' . $options['gapp_wid'], | |
'start-date' => $startDate, | |
'end-date' => date('Y-m-d'), | |
'metrics' => $gapp_metric) | |
, false); | |
if (isset($json->totalsForAllResults->{$gapp_metric})) { | |
$totalResult = $json->totalsForAllResults->{$gapp_metric}; | |
set_transient($gaTransName, $totalResult, 60 * $options['gapp_cache']); | |
return ($format) ? number_format_i18n($totalResult) : $totalResult; | |
} else { | |
$default_value = $options['gapp_defaultval']; | |
set_transient($gaTransName, $default_value, 60 * $options['gapp_cache']); | |
return $options['gapp_defaultval']; | |
} | |
} | |
} | |
function mxp_show_ga_data($atts) { | |
extract(shortcode_atts(array( | |
'format' => true, | |
), $atts)); | |
$content = '<div class="mxp_ga_data">'; | |
$content .= '<div class="today">今日人氣:<span class="mxp_ga_today">' . gapp_get_today_pageviews($format) . '</span></div>'; | |
$content .= '<div class="total">累計人氣:<span class="mxp_ga_total">' . gapp_get_total_pageviews($format) . '</span></div></div>'; | |
return $content; | |
} | |
add_shortcode('mxp_show_me_ga', 'mxp_show_ga_data'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment