PHP - Moved: Google Analytics Php Api
This topic has been moved to Other Libraries and Frameworks.
http://www.phpfreaks.com/forums/index.php?topic=356960.0 Similar TutorialsThis topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=356052.0 I've got a page which has a 301 Redirect. Is there anyway to put the google analytics tracking codes on the page ? I've tried, but because its output, it doesn't work before the 301 Header Redirect. Don't think there is, but worth a try. Cheers. This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=344076.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=315333.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=321605.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=307094.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=333650.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=328756.0 This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=357315.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=307157.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=306254.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=351914.0 Hi everyone, I have a data analysis system which, however, takes too long to insert data into the db. Can you help me understand where I went wrong? thank you DB MYSQL:
1 analytics_number bigint(20) AUTO_INCREMENT --------------------------------------------------------------------------- CLASS PHP Analytics.php class analytics {
public $search_sites; public function get_analytics_enter($visitor_ip, $visitor_visitor_id, $visitor_session_id, $visitor_pageview_id, $visitor_referrer, $visitor_url, $visitor_resolution, $visitor_viewport, $visitor_document){ try{
$GET_array = array("visitor_ip" => $visitor_ip, "visitor_visitor_id" => $visitor_visitor_id, "visitor_session_id" => $visitor_session_id, "visitor_pageview_id" => $visitor_pageview_id, "visitor_referrer" => $visitor_referrer, "visitor_url" => $visitor_url, "visitor_resolution" => $visitor_resolution, "visitor_viewport" => $visitor_viewport, "visitor_document" => $visitor_document);
$query = "SELECT * FROM analytics WHERE visitor_visitor_id = '".$visitor_visitor_id."'"; $visitor_ip_info = get_data_visitor($visitor_ip);
$visitor_visitor_pageview = 0;
$session_buffer = array();
$visitor_visitor_pages = array();
$analytics["visitor_session_landingpage"] = NULL;
foreach ($visitor_data as $visitor) {
if (!in_array($visitor["visitor_page_path"], $visitor_visitor_pages)) {
if ($visitor["visitor_session_id"] == $visitor_session_id) {
if ($visitor_session_pageview == 1) {
if (!in_array($visitor["visitor_page_path"],$visitor_session_pages)) {
if (!in_array($visitor["visitor_session_id"],$session_buffer)) {
if (!in_array($visitor_session_id,$session_buffer)) {
foreach($GET_array as $key => $value) {
$analytics["visitor_visitor_sessions"] = $visitor_visitor_session;
$analytics["visitor_visitor_pageviews"] = $visitor_visitor_pageview + 1; $analytics["visitor_visitor_time"] = "0";
$analytics["visitor_session_pageviews"] = $visitor_session_pageview + 1;
$analytics["visitor_enter_timestamp"] = date("d.m.Y, H:i:s");
$analytics["visitor_leave_timestamp"] = date("d.m.Y, H:i:s"); $analytics["visitor_pageview_time"] = "0";
$analytics["visitor_country"] = $visitor_ip_info["country"];
$analytics["visitor_referrer_url"] = $visitor_referrer;
$analytics["visitor_domain"] = $this->get_domain($visitor_url);
if (!in_array($analytics["visitor_page_path"], $visitor_visitor_pages)) {
if (!in_array($analytics["visitor_page_path"], $visitor_session_pages)) {
$analytics["visitor_visitor_pages"] = json_encode($visitor_visitor_pages);
if ($analytics["visitor_session_pageview"] == 1) {
$sql_1_1 = "UPDATE analytics SET " . "visitor_visitor_last_page" . "='" . "" . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'";
$sql_1 = $sql_1_1."; ".$sql_1_2.";";
$analytics["visitor_visitor_last_page"] = "true";
$analytics["visitor_device"] = visitor_device(); $analytics["visitor_pageview_update"] = date("YmdHis");
$sql_2_1 = "INSERT INTO analytics ("; $this->_db->exec($sql_2_1); $sql_2_2 = "UPDATE analytics SET " . "visitor_visitor_sessions" . "='" . $analytics["visitor_visitor_sessions"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'"; $sql_2_3 = "UPDATE analytics SET " . "visitor_visitor_pageviews" . "='" . $analytics["visitor_visitor_pageviews"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'"; $sql_2_4 = "UPDATE analytics SET " . "visitor_visitor_pages" . "='" . $analytics["visitor_visitor_pages"] . "' WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "'"; $sql_2_5 = "UPDATE analytics SET " . "visitor_session_pageviews" . "='" . $analytics["visitor_session_pageviews"] . "' WHERE " . "visitor_session_id" . "='" . $visitor_session_id."'"; $sql_2_6 = "UPDATE analytics SET " . "visitor_session_pages" . "='" . $analytics["visitor_session_pages"] . "' WHERE " . "visitor_session_id" . "='" . $visitor_session_id."'"; $sql_2_7 = "UPDATE analytics SET " . "visitor_session_landingpage" . "='" . $analytics["visitor_session_landingpage"] . "' WHERE " . "visitor_session_id" . "='" . $visitor_session_id."'"; $sql_2_8 = "UPDATE analytics SET " . "visitor_session_exitpage" . "='" . $analytics["visitor_session_exitpage"] . "' WHERE " . "visitor_session_id" . "='" . $visitor_session_id."'";
$sql_2 = $sql_2_2.";".$sql_2_3.";".$sql_2_4.";".$sql_2_5.";".$sql_2_6.";".$sql_2_7.";".$sql_2_8.";";
} catch(PDOException $e) { public function get_analytics_update($visitor_visitor_id, $visitor_session_id, $visitor_pageview_id, $visitor_pageview_time, $visitor_leave_url){
try{ $query = "SELECT MAX(" . "visitor_session_time" . ") FROM analytics WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "' AND " . "visitor_session_id" . "='" . $visitor_session_id . "' ORDER BY " . "visitor_enter_id" . " DESC LIMIT 1"; $visitor_session_time = $this->_db->query($query)->fetchColumn();
$query = "SELECT MAX(" . "visitor_pageview_time" . ") FROM analytics WHERE " . "visitor_visitor_id" . "='" . $visitor_visitor_id . "' AND " . "visitor_session_id" . "='" . $visitor_session_id . "' AND " . "visitor_pageview_id" . "='" . $visitor_pageview_id . "' ORDER BY " . "visitor_enter_id" . " DESC LIMIT 1"; $analytics = array();
foreach($GET_array as $key => $value) {
$analytics["visitor_leave_timestamp"] = date("d.m.Y, H:i:s");
$analytics["visitor_visitor_time"] = $visitor_visitor_time + ($visitor_pageview_time - $visitor_pageview_time); $analytics["visitor_pageview_update"] = date("YmdHis");
if (isset($visitor_leave_url)) {
$sql_1_1 = "UPDATE analytics SET ";
} catch(PDOException $e) {
function visitor_referrer_type($referrer) {
if ($referrer == "") {
$referrer = explode(".",$referrer);
if (array_key_exists($referrer,$search_sites)) {
function visitor_referrer_name($referrer) {
if ($referrer == "") {
$referrer = explode(".",$referrer);
if (array_key_exists($referrer,$search_sites)) {
function visitor_leave_type($leave_url)
if ($leave_url == "") {
if (strpos($leave_url, $url_paz) !== false) {
function visitor_leave_name($leave_url) $leave_url = $this->get_domain($leave_url);
$leave_url = explode(".",$leave_url); } ?> analytics-db.php
<?php // Dataset IDs
array_push($database_structure["analytics"],"visitor_visitor_id"); // Dataset Visitor Data
array_push($database_structure["analytics"],"visitor_visitor_sessions"); // Dataset Session Data
array_push($database_structure["analytics"],"visitor_session_pageviews"); // Dataset Pageview Data // Dataset Enter Time
array_push($database_structure["analytics"],"visitor_enter_timestamp"); // Dataset Leave Time
array_push($database_structure["analytics"],"visitor_leave_timestamp"); // Dataset Geolocation
array_push($database_structure["analytics"],"visitor_country"); // Dataset Technology
array_push($database_structure["analytics"],"visitor_device"); // Dataset Referrer
array_push($database_structure["analytics"],"visitor_referrer_url"); // Dataset Page
array_push($database_structure["analytics"],"visitor_url"); // Dataset Time array_push($database_structure["analytics"],"visitor_pageview_time"); // Dataset Leave
array_push($database_structure["analytics"],"visitor_leave_url"); // Dataset Update
array_push($database_structure["analytics"],"visitor_pageview_update"); analytics-enter.php <?php
if(isset($_POST["visitor_ip"])){ ?> analytics-update.php <?php $analytics->get_analytics_update($_POST["visitor_visitor_id"], $_POST["visitor_session_id"], $_POST["visitor_pageview_id"], $_POST["visitor_pageview_time"], $_POST["visitor_leave_url"]); ?> JS FILE $(function(){
function create_cookie(name,value,days) {
var host = window.location.hostname;
document.cookie = name+"="+value+expires+"; path=/; domain=." + domain;
function read_cookie(name) {
while (c.charAt(0) == ' ') {
if (c.indexOf(nameEQ) == 0) {
function random_string(length, chars) {
function create_id() {
function create_visitor_visitor_id() {
function visitor_visitor_id() {
function create_visitor_session_id() {
function visitor_session_id() {
if (new URL(window.location.href).searchParams.get("analytics") != "true") { var analytics_script_location = "/analytics/controllers/";
var screen_width = screen.width;
var viewport_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var document_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var visitor_ip;
var visitor_visitor_id = visitor_visitor_id();
var visitor_enter_time = new Date();
var visitor_resolution = screen_width + "x" + screen_height; var visitor_leave_url = "NULL"; var current_time = 0;
var scroll_sum = 0;
var visitor_enter_sent = false;
var current_viewport_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var current_viewport = current_viewport_width + "x" + current_viewport_height;
var current_document = current_document_width + "x" + current_document_height;
function track_a_href(event) {
function analytics_visitor_enter() {
$.ajax({
function analytics_visitor_update() {
$.ajax({
window.setInterval(function() {
function analytics_visitor_leave() {
function visitor_leave_delay(time) {
$(window).load(function() { analytics_visitor_enter(); }); } }); This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=356789.0 This topic has been Ctrl+X/Ctrl+V'd to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=347400.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=346255.0 Hi guys Mods please move if this is the wrong place to ask. I have a google maps api added to my web site but i want to be able to position "pins" on the map to show all the the registered clubs in an area... eg the user searches dublin and all teh clubs pop up... My question is.. is there anyway to do this without needing the user to supply the long/lat.. ie can it be gotten automatically?? I really just want it to work off the addrees/area they supplied when they registered. thanks tt For every search, I get ZERO RESULTS, and I don't understand what I'm doing wrong. Code: [Select] <?php require('places_class.php'); $types = $_POST['type']; $location = $_POST['place']; $radius = $_POST['radius']; $gplaces = New GooglePlaces; $gplaces->SetLocation("$location"); $gplaces->SetRadius($radius); $gplaces->SetTypes("$types"); $results = $gplaces->Search(); print_r($results); ?> That's when the form is submitted, I checked the variables, and they are echoing out correctly. Then I thought I'd hard-code the values in, same results Code: [Select] <?php require('places_class.php'); $gplaces = New GooglePlaces; $gplaces->SetLocation("40.5267,81.4778"); $gplaces->SetRadius(50); $gplaces->SetTypes("food"); $results = $gplaces->Search(); print_r($results); ?> Here is the class Code: [Select] <?php class GooglePlaces { private $APIKey = ""; public $OutputType = "json"; //either json, xml or array public $Errors = array(); private $APIUrl = "https://maps.googleapis.com/maps/api/place"; private $APICallType = ""; private $IncludeDetails = false; //all calls private $Language = 'en'; //optional - https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 //Search private $Location; //REQUIRED - This must be provided as a google.maps.LatLng object. private $Radius; //REQUIRED private $Types; //optional - separate tyep with pipe symbol http://code.google.com/apis/maps/documentation/places/supported_types.html private $Name; //optional //Search, Details, private $Sensor = 'false'; //REQUIRED - is $Location coming from a sensor? like GPS? //Details & Delete private $Reference; //Add private $Accuracy; public function Search() { $this->APICallType = "search"; return $this->APICall(); } public function Details() { $this->APICallType = "details"; return $this->APICall(); } public function Checkin() { $this->APICallType = "checkin-in"; return $this->APICall(); } public function Add() { $this->APICallType = "add"; return $this->APICall(); } public function Delete() { $this->APICallType = "delete"; return $this->APICall(); } public function SetLocation($Location) { $this->Location = $Location; } public function SetRadius($Radius) { $this->Radius = $Radius; } public function SetTypes($Types) { $this->Types = $Types; } public function SetLanguage($Language) { $this->Language = $Language; } public function SetName($Name) { $this->Name = $Name; } public function SetSensor($Sensor) { $this->Sensor = $Sensor; } public function SetReference($Reference) { $this->Reference = $Reference; } public function SetAccuracy($Accuracy) { $this->Accuracy = $Accuracy; } public function SetIncludeDetails($IncludeDetails) { $this->IncludeDetails = $IncludeDetails; } private function CheckForErrors() { if(empty($this->APICallType)) { $this->Errors[] = "API Call Type is required but is missing."; } if(empty($this->APIKey)) { $this->Errors[] = "API Key is is required but is missing."; } if(($this->OutputType!="json") && ($this->OutputType!="xml") && ($this->OutputType!="json")) { $this->Errors[] = "OutputType is required but is missing."; } } private function APICall() { $this->CheckForErrors(); if($this->APICallType=="add" || $this->APICallType=="delete") { $URLToPostTo = $this->APIUrl."/".$this->APICallType."/".$this->OutputType."?key=".$this->APIKey."&sensor=".$this->Sensor; if($this->APICallType=="add") { $LocationArray = explode(",", $this->Location); $lat = trim($LocationArray[0]); $lng = trim($LocationArray[1]); $paramstopost[location][lat] = $lat; $paramstopost[location][lng] = $lng; $paramstopost[accuracy] = $this->Accuracy; $paramstopost[name] = $this->Name; $paramstopost[types] = explode("|", $this->Types); $paramstopost[language] = $this->Language; } if($this->APICallType=="delete") { $paramstopost[reference] = $this->Reference; } $result = json_decode($this->CurlCall($URLToPostTo,json_encode($paramstopost))); $result->errors = $this->Errors; return $result; } if($this->APICallType=="search") { $URLparams = "location=".$this->Location."&radius=".$this->Radius."&types=".$this->Types."&language=".$this->Language."&name=".$this->Name."&sensor=".$this->Sensor; } if($this->APICallType=="details") { $URLparams = "reference=".$this->Reference."&language=".$this->Language."&sensor=".$this->Sensor; } if($this->APICallType=="check-in") { $URLparams = "reference=".$this->Reference."&language=".$this->Language."&sensor=".$this->Sensor; } $URLToCall = $this->APIUrl."/".$this->APICallType."/".$this->OutputType."?key=".$this->APIKey."&".$URLparams; $result = json_decode(file_get_contents($URLToCall),true); $result[errors] = $this->Errors; if($result[status]=="OK" && $this->APICallType=="details") { foreach($result[result][address_components] as $key=>$component) { if($component[types][0]=="street_number") { $address_street_number = $component[short_name]; } if($component[types][0]=="route") { $address_street_name = $component[short_name]; } if($component[types][0]=="locality") { $address_city = $component[short_name]; } if($component[types][0]=="administrative_area_level_1") { $address_state = $component[short_name]; } if($component[types][0]=="postal_code") { $address_postal_code = $component[short_name]; } } $result[result][address_fixed][street_number] = $address_street_number; $result[result][address_fixed][address_street_name] = $address_street_name; $result[result][address_fixed][address_city] = $address_city; $result[result][address_fixed][address_state] = $address_state; $result[result][address_fixed][address_postal_code] = $address_postal_code; } return $result; } private function CurlCall($url,$topost) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $topost); $body = curl_exec($ch); curl_close($ch); return $body; } } ?> And yes, I do have the correct API Key, I just took it out Can anybody see what the issue is? Hey guys, I have stored a google map link into a database. When I try to display it from database using the command below <?php echo $google_map; ?> I get this output, but I can't display a map. Code: [Select] <iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=hr&geocode=&q=smi%C4%8Diklasova+3,+karlovac,+croatia&sll=37.0625,-95.677068&sspn=50.910968,135.263672&ie=UTF8&hq=&hnear=Tadije+Smi%C4%8Diklasa,+Karlovac,+Republika+Hrvatska&t=h&ll=45.487155,15.55089&spn=0.028883,0.054932&z=14&iwloc=A&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=hr&geocode=&q=smi%C4%8Diklasova+3,+karlovac,+croatia&sll=37.0625,-95.677068&sspn=50.910968,135.263672&ie=UTF8&hq=&hnear=Tadije+Smi%C4%8Diklasa,+Karlovac,+Republika+Hrvatska&t=h&ll=45.487155,15.55089&spn=0.028883,0.054932&z=14&iwloc=A" style="color:#0000FF;text-align:left">Prikaz veće karte</a></small> Can you tell me why?? When try to copy that directly to HTML it works perfectly!!! I'm confused! i found the following code he https://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script> contry_code = google.loader.ClientLocation.address.country_code city = google.loader.ClientLocation.address.city region = google.loader.ClientLocation.address.region </script> will this work to get location info? Edited October 15, 2019 by ajetrumpet |