JS file
function getPage(action,rel_id,detail) {
const js_message_show = document.getElementById("js_message");
var person_id = getCookie('person_id');
const data = {
os:getOS(),
language:navigator.language ,
browser:getBowser(),
action:action,
rel_id:rel_id,
detail:detail,
person_id:person_id
};
fetch('https://xxxxxx.com/api...../', {
method: 'POST', // or 'PUT'
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
mode: 'cors',
body: JSON.stringify(data),
})
.then((data) => data.json())
.then((data) => {
if(data.n_id > 0){
// document.write( 'hi data <hr>' +data.n_id);
setCookie('person_id',data.n_id,730)
}
})
.catch((error) => {
// js_message_show.innerText = error
// console.error('Error:', error);
});
}
function getBowser() {
if ((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1) {
return 'Opera'
} else if (navigator.userAgent.indexOf("Edg") != -1) {
return 'Edge'
} else if (navigator.userAgent.indexOf("Chrome") != -1) {
return 'Chrome'
} else if (navigator.userAgent.indexOf("Safari") != -1) {
return 'Safari'
} else if (navigator.userAgent.indexOf("Firefox") != -1) {
return 'Firefox'
} else if ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true)) //IF IE > 10
{
return 'IE'
} else {
return 'unknown'
}
}
function getOS() {
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'MacOS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (!os && /Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = 'expires='+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
Laravel API controller
<?php
namespace App\Http\Controllers;
use App\Models\View;
use Illuminate\Http\Request;
class AnclyticsController extends Controller {
protected $view_path = 'topic';
protected $route_name = 'topic';
public function main(Request $request, View $view) {
//check limit
$check = '';
if (is_numeric($request->person_id) && $request->action && $request->rel_id) {
$check = View::where('person_id', $request->person_id)
->where('action', $request->action)
->where('rel_id', $request->rel_id)
->orderBy('created_at', 'desc')
->first();
}
if ($check == '' || strtotime($check->created_at) < strtotime("-1 minutes")) {
$person_id = $request->person_id;
if ($person_id == '' || $person_id == null || !is_numeric($person_id)) {
$new_person = View::select('person_id')->orderBy('person_id', 'desc')->first();
$new_person_id = ($new_person->person_id + 1);
$person_id = ($new_person->person_id + 1);
} else {
$new_person_id = null;
}
$view->ip = $this->getIp();
$view->os = $request->os;
$view->language = $request->language;
$view->browser = $request->browser;
$view->action = $request->action;
$view->rel_id = $request->rel_id;
$view->detail = json_encode($request->detail);
$view->person_id = $person_id;
$view->save();
$message = [
'person_id' => $request->person_id,
'n_id' => $new_person_id,
];
// $message = ;
return json_encode($message);
} else {
$message = [
'person_id' => null,
'n_id' => null,
'no' => 'yes',
];
return json_encode($message);
}
}
public function getIp() {
if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
}
return $_SERVER['REMOTE_ADDR'];
}
}
Laravel Model/View.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class View extends Model {
use HasFactory;
protected $table = 'views';
// public $timestamps = false;
protected $fillable = [
'ip',
'person_id',
'action',
'rel_id',
'os',
'language',
'browser',
'detail',
];
}