How to Add Google Analytics With Custom PHP Website

by Ahmed Khan  January 5, 2016

For a business owner, customers are the most important assets. It is a priority for a business owner to get awareness about the audience visiting their websites so that they can optimize their website accordingly to attract more visitors. There a number of analytics software that provide these services for free; however, the best to stand out is Google Analytics (GA).

integrating google

Google analytics enables you to:

  • Get information about the visitor’s geographical location, web browser’s information, resolution of their screen and the languages they speak.
  • Track their activity on your website, average page views and most viewed pages, etc.
  • Track the visiting time of your audience. With this information, you can schedule your posts accordingly.
  • Track the referral path as well. For example are the visitors coming from social media or organic?
  • Track the keywords through which visitors are coming towards your website.
  • Track the clicks on specific URLs and much more.

Google Analytics is currently the most powerful analytics tool. Businesses which really want to expand using the internet should opt for Google Analytics. It is free to use with full support and documentation available to utilize it perfectly. You may add different dimensions and metrics in your report. For more advanced users, there are Google Analytics API that are also easily available. You can easily connect webmaster tools to it. In short, Google Analytics will help you exploit your data to your advantage.

Method of Adding Google Analytics Code to Your Website

To add Google Analytics to your website, you first need to have a Google Account. If you still don’t have one, then all you need to do is head over to Google Analytics and signup for a new Google Account. After making an account, sign in and add Google Analytics to your account. Enter the name of the account, Site URL, and other details.

After you are done setting it up, you now need to collect Google analytics code to begin tracking of your domain. Asynchronous Google analytics code selection is recommended as it reduces the chance of a lag in your server due to a third party intervention. The asynchronous Google analytics code should be added to the <head> of your site. You can add additional scripts to the <head> as well. You should embed Google analytics code on every page you want to track.

Alternative Method

The above method is the easiest method for deployment of Google Analytics. Alternatively, Google Analytics tracking code can also be added via a custom function as well. In this way, all your modifications will be available on a single file custom_functions.php. There is no significant advantage of using this method; however, if you want to integrate analytics through function rather than thesis interface then this method will suit you. You should include this file on every page you want to track.

In the above code, replace UA-XXXXXXX with the unique tracking code. Google Analytics code is pasted within the script tag of the function, and only the UA-XXXXXXXX will change.

Your Google Analytics is now deployed for your website. You can now visit your Google Analytics dashboard; however, you will not see the metrics for a while as the tracking takes a bit of time.

Integrate Google Analytics With Custom PHP Website

Till then, you should acquaint yourself with the Google Analytics platform and get used to the tracking metrics. You are about to board a data train that is going to help you take your business to the next big leap. Happy tracking!

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...