Google Analytics for your Web Application

A Guide on How To Add Google Analytics to your Web Application

Blue Light Tech

Google Analytics offers detailed statistics related to sales for your website as well as visitor traffic. This tutorial can be beneficial to any website owner who’d like to analyse and grow their visitor base!

Google Analytics provides a way to add the tracking code, but if you’re not using Server Side Includes, PHP includes or another form of layout template, the process might get tedious and inefficient.  Depending on your website’s set-up, we’ll provide two alternatives to insert the Google Analytics tracking code to your website.

Important Note:
Steps below require root privileges.

  1. Signing Up for Google Analytics
  2. Add Through PHP
  3. Add Through External JavaScript

Signing Up for Google Analytics

You need to sign up and setup your Google Analytics account before adding it to your website:

  1. Visit Google Analytics (, click on Access Google Analytics and click on Sign Up
  2. Make sure the Website option is selected. Enter your account information and make sure the website URL is accurate.
  3. Click on Get Tracking ID. Read and accept Google Analytics Terms of Services.
  4. Your Tracking ID and Tracking Code will be sent to you. Write them down as you will need these later.

You can add this code to your website through PHP, or an external JavaScript file.

Add Through PHP

You can add the tracking code through a PHP script if your website is coded as such. This can be useful if you’re not using a separate PHP file for your header, or otherwise want to keep the code itself outside your header file. Since you will only have to edit one file, this will make any changes to the tracking code more efficient:

  1. Go to the directory that your website is hosted in:
    cd /var/www/



    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    ga('create', 'UA-00000000-0', 'auto');
    ga('send', 'pageview');

    (Important Note: Substitute UA-00000000-0 for your Tracking ID)

  2. Copy your tracking code and create a file named googleanalytics.php
  3. If you need to insert the code in every page, and your website does not have a separate header file, skip to step 6. Otherwise after your <body> tag, open and add the code to your header document.

    File excerpt:

    <?php include_once('googleanalytics.php') ?>
  4. You have successfully added Google Analytics to your website. It may take up to 24 hours for the data to show up on Google Analytics. You don’t need to follow the rest of this tutorial.

  5. You can insert the needed code to the terminal of your PHP-enabled website if it doesn’t have a header template. Be sure of being in the directory that holds your website.
    sed -i 's/<body>/<body><?php include_once('googleanalytics.php') ?>/g' *.php
  6. In order to see if the code was successfully inserted or not, you can open your website in your browser and view the source file in the terminal. You should see the inserted code immediately after <body> tag.

    File Excerpt:


    <body><?php include_once('googleanalytics.php') ?>

    Please note: It might take up to 24 hours for any data to show up on Google Analytics.

Add Through External JavaScript

Your website can’t use PHP if its files end with .html and .htm. By using an external JavaScript file and the sed command, you can insert the Google Analytic code through the terminal.

  1. Visit the directory your website is hosted in:
    cd /var/www/
  2. Create a JavaScript folder. If you have one, you can use it (This step is optional)
      mkdir javascript
  3. Go to the folder you made:
     cd javascript
  4. To hold your Google Analytics code, create a ga.js file.

    Insert the following code. (Replace tracking ID with UA-00000000-0)

    File Excrept:


    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    ga('create', 'UA-00000000-0', 'auto');
    ga('send', 'pageview');
  5. sed command will help you to insert a link to the JavaScript file holding your tracking code. Search and replace all instances of your <head> tag with the code below
    sed -i 's@<head>@<head><script type='text/javascript' src='javascript/ga.js'></script>@g' *.html
  6. 5-      To see if the code has been successfully inserted into your html files, open your website in a browser, view the source code, or view any file in terminal.

    File Excerpt:


    <head><script type='text/javascript' src='javascript/ga.js'></script>

And that’s it! You have successfully added Google Analytics to your website!


GoldenTabs - 08/01/2018 13:48:12


GoldenTabs - 05/01/2018 20:06:09


Warren - 06/12/2017 14:04:55

When can you start? uneasiness avanafil slumber His speech, under rains that have persisted throughout most of his first trip abroad as pope, came halfway through a week-long visit around World Youth Day, a gathering of young Catholics that is expected to attract more than a million faithful to Rio de Janeiro and nearby sites.

JimmiXzSq - 21/05/2017 00:19:27


JimmiXzSq - 18/05/2017 17:08:00


JimmiXzSq - 18/05/2017 15:49:05


Barnypok - 31/03/2017 18:48:16


JimmiXzSw - 18/02/2017 20:45:32


Angga - 20/12/2015 22:45:50

Good post. I study one thing more difficult on coplmetely different blogs everyday. It is going to always be stimulating to learn content from other writers and apply a bit one thing from their store. I’d choose to make use of some with the content material on my weblog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

Phillip G - 03/11/2015 13:19:26

Useful guide, and a really useful tool once set up. Being able to delve into where visitors have come from will be used extensively as I begin this marketing campaign. Thanks for the guide!