Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

PHP

Build your own Google Analytics easily using PHP

By Admin
July 10, 2024 6 Min Read
0

In this tutorial, you will be learning to create your own Google Analytics like tool using PHP.

Google Analytics is a powerful web analytics service offered by Google that allows website owners and marketers to track and analyze various aspects of their website or app traffic.

We need 4 files :

  • get.php => contains SQL queries that will be used by the index.php to show data
  • index.php => dashboard page to see realtime stats
  • track.js => JavaScript file that is included in the webpage whose analytics is to be tracked
  • track.php => PHP script that receives analytics data from webpage and stores in database

MySQL Table for your own Google Analytics

Create a table “visits” with following SQL code

CREATE TABLE `visits` (
  `id` int(11) NOT NULL,
  `path` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `device` varchar(25) NOT NULL,
  `source` varchar(255) NOT NULL,
  `user_session` varchar(100) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `country` varchar(255) NOT NULL,
  `city` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

ALTER TABLE `visits`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `visits`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
COMMIT;
Table Structure for Your Own Google Analytics

Follow this video for complete guidance :

https://www.youtube.com/watch?v=U8tLIK6B8xs

Complete Source Code

track.js

(() => {
	var postData = {
		'path' : window.location.pathname,
		'title': document.title,
		'device':getDeviceType(),
		'source':getTrafficSource(),
		'user_session':getUserSession(),
		'country':getCountry(),
		'city':getCity()
	};
	
	postData = JSON.stringify(postData);
	xhr = new XMLHttpRequest();
	xhr.open('POST', 'http://localhost/ana/track.php', true);
	xhr.setRequestHeader('Content-type', 'application/json; charset=UTF-8');
	xhr.send(postData);
	xhr.onload = function () {
	    if(xhr.status === 200) {
	    	response = JSON.parse(xhr.response);
	    	console.log(response);
	    }
	}

	function getDeviceType() {
	    const userAgent = navigator.userAgent;
	    if (/android|iphone|ipad|ipod|mobile|mini|windows\sce|palm/i.test(userAgent)) {
	        return "Mobile";
	    }
	    if (/tablet|ipad|playbook|silk|android(?!.*mobi)/i.test(userAgent)) {
	        return "Tablet";
	    }
	    return "Desktop";
	}

	function getTrafficSource() {
	    const referrer = document.referrer;
	    const searchEngines = ["google", "bing", "yahoo", "duckduckgo", "baidu"];
	    const socialNetworks = ["facebook", "twitter", "linkedin", "instagram", "pinterest"];
	    if (!referrer) {
	        return "Direct";
	    }
	    const referrerHost = new URL(referrer).hostname.toLowerCase();
	    for (const engine of searchEngines) {
	        if (referrerHost.includes(engine)) {
	            return "Organic : "+referrerHost;
	        }
	    }
	    for (const network of socialNetworks) {
	        if (referrerHost.includes(network)) {
	            return "Social : "+referrerHost;
	        }
	    }
	    return "Referral :" + referrerHost;
	}

	function getUserSession(){
		if(localStorage.getItem('user_session')){
			return localStorage.getItem('user_session');
		}else{
			session_id = generateRandomString(25);
			localStorage.setItem('user_session',session_id);
			return session_id;
		}
	}

	function generateRandomString(length) {
	    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
	    let result = '';
	    const charactersLength = characters.length;
	    for (let i = 0; i < length; i++) {
	        result += characters.charAt(Math.floor(Math.random() * charactersLength));
	    }
	    return result;
	}

	function getCountry(){
		return 'Nepal';
	}

	function getCity(){
		return 'Kathmandu';
	}
})();

track.php

<?php
$conn = mysqli_connect('HOST','USER','PASSWORD','DATABASE_NAME');
date_default_timezone_set('Asia/Kathmandu');


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'),true);

    $path = $data['path'];
    $title = $data['title'];
    $device = $data['device'];
    $source = $data['source'];
    $user_session = $data['user_session'];
    $created_at = date('Y-m-d H:i:s');
    $country = $data['country'];
    $city = $data['city'];
    
    $sql = "insert into visits(path,title,device,source,user_session,created_at,country,city) values('$path','$title','$device','$source','$user_session','$created_at','$country','$city')";
    mysqli_query($conn,$sql);
}

index.php

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<style type="text/css">
	.box{
		background: beige;
		height: 200px;
		padding: 20px;
		position: relative;
	}
	#map {
        height: 100%;
    }
    .leaflet-control-attribution{
    	display: none;
    }
</style>
<div class="container-fluid">
	<div class="header text-center pt-3">
		<h1>Realtime Website Analytics</h1>
	</div>
</div>
<hr>

<div class="container">
	<div class="row">
		<div class="col-md-3">
			<div class="box user-count fs-3 d-flex text-center justify-content-center align-items-center">
				<div>
					<div id="active-users"></div>
					<div class="fs-5">Active Users</div>
				</div>
			</div>
		</div>
		<div class="col-md-6">
			<div class="box pb-0">
				<small class="position-absolute" style="padding:0px 10px;background:#444;color:#fff;top:0;left:50%;transform: translate(-50%,0);">Page Views in Last 30 minutes</small>
				<canvas id="realtime-chart"></canvas>
			</div>
		</div>
		<div class="col-md-3">
			<div class="box p-0">
				<div id="map"></div>
			</div>
		</div>
	</div>

	<div class="mt-5">
		<div class="row">
			<div class="col-md-8">
				<div class="box">
					<h3 class="text-center mb-4">Active Pages</h3>
					<table class="table table-bordered" id="active-pages">
						<thead>
							<tr>
								<th width="80%">Top Active Pages</th>
								<th>Views</th>
							</tr>
						</thead>
						<tbody>
							
						</tbody>
					</table>
				</div>
			</div>
			<div class="col-md-4">
				<div class="box">
					<h3 class="text-center mb-4">Traffic Source</h3>
					<table class="table table-bordered" id="traffic-source">
						<thead>
							<tr>
								<th width="80%">Top Active Pages</th>
								<th>Views</th>
							</tr>
						</thead>
						<tbody>
							
						</tbody>
					</table>
				</div>
			</div>
		</div>
		
	</div>
</div>

        <script src="https://analytics.youthsforum.com/themes/altum/assets/js/libraries/Chart.bundle.min.js"></script>
<script src="https://analytics.youthsforum.com/themes/altum/assets/js/chartjs_defaults.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.min.js"></script>

<script type="text/javascript">

		function refresh(){
			$.ajax({
				url:'get.php',
				type:'get',
				success:function(res){
					res = JSON.parse(res);
					$("#active-users").html(res.active_users);

					tr = '';
					for(i=0;i<res.active_pages.length;i++){
						tr += '<tr>'+
									'<td>'+res.active_pages[i].title+'</td>'+
									'<td>'+res.active_pages[i].page_views+'</td>'+
								'</tr>';
					}
					$("table#active-pages tbody").html(tr);

					initBar(res.active_minutes);

					tr = '';
					for(i=0;i<res.traffic_sources.length;i++){
						tr += '<tr>'+
									'<td>'+res.traffic_sources[i].source+'</td>'+
									'<td>'+res.traffic_sources[i].users+'</td>'+
								'</tr>';
					}
					$("table#traffic-source tbody").html(tr);

				}
			});
		}

		setInterval(refresh,5000);

        var map = L.map('map', {
            zoomControl: false, // Disable default zoom control
            continuousWorld: false
        }).setView([27.7172, 85.3240], 1);

        
        function addCountryMarker(countryName, latlng) {
            var marker = L.marker(latlng).addTo(map);

            marker.bindPopup('<b>' + countryName + '</b>');
        }

        // Example: Adding a marker for Nepal
        var nepalLatLng = [28.3949, 84.1240]; // Example coordinates for Nepal
        addCountryMarker('Nepal : 4', nepalLatLng);

		L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: false // Disable default attribution
        }).addTo(map);

	var ctx = document.getElementById("realtime-chart").getContext('2d');

	function initBar(data){
	    var myChart = new Chart(ctx, {
	        type: 'bar',
	        data: {
	            labels: ["", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", "","", "", "", "", "", "", "", "", "", ""],
	            datasets: [{
	                label: '', // Empty string to hide the dataset label
	                data: data,
	                fill: false,
	                borderColor: '#2196f3',
	                backgroundColor: '#2196f3',
	                borderWidth: 1,
	                barPercentage : 1.24
	            }]
	        },
	        options: {
	            responsive: true,
	            maintainAspectRatio: false,
	            plugins: {
	                legend: {
	                    display: false // Disable the dataset label
	                },
	                tooltip: {
	                    enabled: false // Disable tooltips
	                }
	            },
	            scales: {
	                y: {
	                    beginAtZero:true,
	                    ticks: {
	                    	stepSize:1
	                    }
	                },
	                x: {
	                	display: false
	                }
	            }
	        }
	    });
	}
	refresh();
</script>

get.php

<?php


$conn = mysqli_connect('HOST','USER','PASSWORD','DATABASE_NAME');


$sql = "SELECT count(distinct(user_session)) as active_users FROM visits WHERE created_at >= NOW() - INTERVAL 30 MINUTE";
$result = mysqli_query($conn,$sql);
$result = mysqli_fetch_assoc($result);
$response['active_users'] = $result['active_users'];


$sql = "SELECT *,count(id) as page_views FROM visits WHERE created_at >= NOW() - INTERVAL 30 MINUTE group by path";
$result = mysqli_query($conn,$sql);
$active_pages = array();
while($row = mysqli_fetch_assoc($result)){
	$active_pages[] = $row;
}
$response['active_pages'] = $active_pages;


$sql = "WITH RECURSIVE IntervalSequence AS ( SELECT NOW() - INTERVAL 29 MINUTE AS interval_start UNION ALL SELECT interval_start + INTERVAL 1 MINUTE FROM IntervalSequence WHERE interval_start < NOW() ) SELECT DATE_FORMAT(IntervalSequence.interval_start, '%Y-%m-%d %H:%i') AS minute_interval, COUNT(visits.id) AS active_users_count FROM IntervalSequence LEFT JOIN visits ON visits.created_at >= IntervalSequence.interval_start AND visits.created_at < IntervalSequence.interval_start + INTERVAL 1 MINUTE GROUP BY minute_interval ORDER BY minute_interval ASC";
$result = mysqli_query($conn,$sql);
$active_minutes = array();
while($row = mysqli_fetch_assoc($result)){
	$active_minutes[] = $row['active_users_count'];
}
$response['active_minutes'] = $active_minutes;


$sql = "select *,count(id) as users from visits WHERE created_at >= NOW() - INTERVAL 30 MINUTE group by source";
$result = mysqli_query($conn,$sql);
$traffic_sources = array();
while($row = mysqli_fetch_assoc($result)){
	$traffic_sources[] = $row;
}
$response['traffic_sources'] = $traffic_sources;

echo json_encode($response);
exit;

Place this tracking code in the website you want to track

<script src="LINK_TO_TRACK.JS"></script>

Key Features of Google Analytics

  • Traffic Analysis: It provides detailed insights into your website’s traffic sources, showing where visitors come from (referral sources, search engines, direct visits), which pages they visit, and how long they stay on your site.
  • Audience Analysis: You can segment your audience based on various factors such as demographics (age, gender), interests, location, and behavior (new vs. returning visitors).
  • Behavior Flow: This feature visualizes how users navigate through your site, showing the path they take from one page to another.
  • Conversion Tracking: You can set up goals to track specific actions users take on your site, such as completing a purchase, signing up for a newsletter, or downloading a file. This helps in measuring the effectiveness of your marketing campaigns.
  • E-commerce Tracking: For online stores, Google Analytics can track transaction data, revenue, and other e-commerce metrics, providing insights into sales performance.
  • Real-Time Data: It offers real-time reporting, showing you active users on your site, their geographic locations, and the pages they are currently viewing.
  • Customization: Google Analytics allows customization through custom dimensions and metrics, which can be used to track specific user interactions or events on your site.
  • Integration: It integrates with other Google products such as Google Ads, Search Console, and Data Studio, allowing for deeper insights and cross-platform analysis.
  • Reporting: It provides a range of pre-built reports and dashboards for quick insights, and you can also create custom reports tailored to your specific needs.
Author

Admin

Follow Me
Other Articles
Place Nepali characters on iPhone's lock screen
Previous

Place Nepali characters on iPhone’s lock screen: complete guidance

Latest 5 Amazing Nepali movie reviews
Next

Latest 5 amazing Nepali movie reviews you can watch: 2024

No Comment! Be the first one.

Leave a Reply

Your email address will not be published. Required fields are marked *

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme