I have a php page that encodes an array into a JSON object or JSONP callback and just echoes the result. Since this will become a separate page that will be directly accessed, I would like to receive information and enable google analytics.
JSON encodes a php page:
<?php
$arr = array('country' => 'United States', 'city' => 'New York');
if(isset ($_GET['jsonp'])) {
echo $_GET['jsonp'] . '(' . json_encode($arr) . ')';
}
else {
echo json_encode($arr);
}
?>
Part of jQuery decoding:
<script>
$.getJSON('https://geoip-db.com/json/geoip.php?jsonp=callback')
.done (function(location)
{
$('#country').html(location.country);
$('#city').html(location.city);
});
</script>
Google recommends creating a separate php file containing the javascript tracking code and including it in all the php pages you want to track. Sort of:
<?php include_once("analyticstracking.php") ?>
, , JSON JQuery , script. php encode - script, JSON.
?