When a user comes to browse your WordPress site, a Web server sends the requested page back to the user’s browser. However, if the user decides that he/she doesn’t want to view your page, they might close the browser even before the page is completely loaded.
Now, how do you track this kind of users? The answer to this is the proper placement of the Google Analytics code (GA Code). The best place to put it in WordPress or custom websites is the header/top of the page. If the code is at the bottom of the page, then we might miss the count as the chances are that the GA code might not render. However, if you place older code at the top of a Web page, it can slow down your site when there are network issues.
Traditional Code
GA makes a request to a 1×1 or let’s say empty image file from its servers. When the request is made, it passes some valuable information about your account and the user’s visit to the server. Traditional tracking code provided from Google makes a synchronous request. This means that WordPress has to wait until the request finishes before it continues loading the Web page. For this very simple reason, the traditional code should be placed at the bottom of a Web page, immediately before the closingtag. By placing the code at the bottom doesn’t slow down your WordPress site.
Asynchronous Code
The current tracking code makes asynchronous calls to the servers. With an asynchronous call, the browser sends the request to Google Analytics and then continues loading the Web page. It doesn’t wait for the request to complete before it moves on to the next task. This enables you to place the tracking code at the top of each Web page, immediately before the closingtag, without slowing down your site. By placing the code at the top of each page, you’re more likely to record visits from users who abandon a page before it completely loads.
Editing Your Theme
Each time WordPress generates a new Web page, it calls the Header script to create the page header, creates the page body using a variety of scripts installed with your theme, and then calls the Footer script to finish creating the page. You can place the Google Analytics code on your WordPress site using the editor provided with the WordPress dashboard under the “Appearances” menu item. Edit the “Header (header.php)” file to place asynchronous code before the closing tag. Edit the “Footer (footer.php)” file to place synchronous code before the closing tag in your theme.
Allowing Your Theme to Place the Code
Many WordPress themes have a built-in feature that simplifies the process of placing your Google Analytics code in the right place. Review the list of features your theme provides or browse the configuration options for the theme to locate a Google Analytics setup field. Enter the website ID assigned by Google Analytics, for example, UA-000000, and allow the theme to automatically insert the Google Analytics code in the proper place on each page. Read the theme specifications to ensure the theme uses asynchronous calls to Google Analytics.
Use Google Analytics Plugins
What if the theme that you are using doesn’t support the feature of placing Google Analytics code in the right place. Well, to answer this in a very simple way, use the WordPress plugins available in the plugins market. The one particular that we are also using is the “Google Analytics Dashboard for WP (GADWP)“.
Source: https://developersjournal.in/best-place-put-google-analytics-wordpress/