你钩住了wp_footer
并添加GA代码。
以下是我将如何做到这一点。
选项1:
获取当前URL,并使用选定的URL进行计数器检查。
add_action(\'wp_footer\', \'wpse388915_custom_ga_code\', 99);
function wpse388915_custom_ga_code() {
$current_url = (is_ssl() ? \'https://\' : \'http://\') . $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
switch($current_url) {
case \'www.example.com/page/path1\':
case \'www.example.com/page/path2\':
case \'www.example.com/page/path3\':
echo \'GA Property Code 1\';
break;
case \'www.example.com/page/path10\':
case \'www.example.com/page/path15\':
case \'www.example.com/page/path16\':
echo \'GA Property Code 2\';
break;
}
}
选项2:
获取当前URL,然后使用
url_to_postid
并使用您选择的帖子/页面ID进行计数器检查。
add_action(\'wp_footer\', \'wpse388915_custom_ga_code\', 99);
function wpse388915_custom_ga_code() {
$current_url = (is_ssl() ? \'https://\' : \'http://\') . $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
$post_id = url_to_postid($current_url);
// Return if page does not exist.
if (!$post_id) return;
$preferred_post_type = get_post_field(\'post_type\', $post_id);
if ($preferred_post_type == \'my_post_type\') {
switch($post_id) {
case 1:
case 2:
case 3:
echo \'GA Property Code 1\';
break;
case 10:
case 15:
case 16:
echo \'GA Property Code 2\';
break;
}
}
}