When the Google Analytics tracking code is installed on the WordPress website, the standard template provided by the Google Analytics management platform will be used to track all the traffic of both logged-in and logged-in users. It is very suitable for e-commerce websites that focus on member behavior tracking. If you just want to track the behavior of your readers and don't want to record the browsing behavior of webmasters or editors, you must use additional settings.
Method XNUMX: Use an off-the-shelf plug-in
Use off-the-shelf plug-ins, such as the Rank Math SEO plug-in, which provides the option to exclude logged-in users (Exclude Logged-in Users), or the Perfmatters performance plug-in, which provides the option to track Logged-in Admins or not , If there are people who use the above plug-in, you can set it directly in the plug-in, but it is not suitable to install a plug-in for this purpose.
Method XNUMX: Refer to the official recommended practice
ReferenceOfficial Google Guidelines, write code on your own website, first mark the user's login status, send the status value to the Google tracking code administrator, and then set up a screening program on the Google analytics platform to exclude the traffic of logged-in users.However, the officially recommended method is too complicated and must be set on both your own website and the Google platform.
Method XNUMX: Install the custom code snippet
Use code snippets (Code Snippet) to manage plug-ins, such as: Code Snippets,WPCodeBoxWait for the plug-in program, modify the original Google Analytics tracking code, adjust it to PHP syntax, identify whether it is a logged-in user, and then decide whether to insert the tracking code, delete the tracking code of the logged-in user browsing the web page, and the data will not be sent to Google Analysis platform.
Legacy Tracking Code Format
The original format of the Google Analytics tracking code is as follows: "G-XXXXXXXXXX" is the unique ID number of each website, which is usually installed on each webpageThe javascript program before the tag is rewritten into PHP code, which can delete the tracking code in the web page of the logged-in user, and the data will not be transmitted to the Google Analytics platform.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
Modified tracking code format
The modified PHP of Google Analytics is like the following example, with a line added in front of the original tracking code" ", add a line after it" ”, you can determine whether it is a logged in user, and delete the tracking code in the web page that the logged in user browses. UseWPCodeBoxWhen setting the plug-in, select the "PHP" type and set the location "wp_head".
<?php if (!is_user_logged_in()) { ?>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>
<?php } ?>
If you want to exclude the traffic of a specific logged in user, you can use "if ($user_id)" instead of "if (!is_user_logged_in())" and set the user ID to filter.