Google: Analytics2KML

Hittade ett inlägg om hur man kan göra om Google Analytics-data till en Google Earth-KML… http://bogomip.net/blog/google-analytics-and-google-earth/

Men… vad ska man med C-kod till när man har PHP :-)

Eftersom inlägget är på engelska och ganska så väl-digg’at så skriver jag vidare på engelska…

Sorry for the Swedish note
Since I’m track-backing the blogentry… I might need to explain this in english.
Well… I wrote a small hack to go from Google Analytics to Google Earth with a little help of PHP.

To use this… go to Google Analytics / Marketing Optimization / Visitor Segment Performance / Geo Map Overlay
Then export to XML. Save the XML-file as analytics2kml.xml and upload it next to the php-files below.

Use the 2 files below to create a nice way to link it from any webpage. You need to create the link to analytics2kml-starter.php

analytics2kml.php


// — Open file
$filename = "analytics2kml.xml";

if (!($fp = fopen($filename, "r"))) {
   die("could not open XML input");
}

$xml_parser = xml_parser_create();
$data = fread($fp, filesize($filename));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

// — Populate array
$xmlArr = array();
$level = array();
foreach ($vals as $xml_elem) {
  if ($xml_elem[‘type’] == ‘open’) {
   if (array_key_exists(‘attributes’,$xml_elem)) {
     list($level[$xml_elem[‘level’]],$extra) = array_values($xml_elem[‘attributes’]);
   } else {
     $level[$xml_elem[‘level’]] = $xml_elem[‘tag’];
   }
  }
  if ($xml_elem[‘type’] == ‘complete’) {
   $start_level = 1;
   $php_stmt = ‘$xmlArr’;
   while($start_level < $xml_elem[‘level’]) {
     $php_stmt .= ‘[$level['.$start_level.']]’;
     $start_level++;
   }
   $php_stmt .= ‘[$xml_elem[\'tag\']] = $xml_elem[\'value\'];’;
   eval($php_stmt);
  }
}

// — Write page headers
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("content-type:application/vnd.google-earth.kml+xml"); // Google Earth

// — Create top of KML
$kml_output =;
$kml_output.=;
$kml_output.="

        Analytics 2 kml


        Analytics 2 kml
        "
;
echo $kml_output;

// — Loop thru array
$initRows = 8;
$totalRows = count($xmlArr["URCHINDATA"]["0"]) - $initRows;

for ($i = 0; $i <= $totalRows; $i++) {
        $pointStr  = $xmlArr["URCHINDATA"]["0"][$i]["NAME"];
        $pointStr .= "|" . $xmlArr["URCHINDATA"]["0"][$i]["VALUE1"];
        $pointStr .= "|" . $xmlArr["URCHINDATA"]["0"][$i]["VALUE2"];

        $pointArr = explode("|", $pointStr);

        $nameArr = explode("-", $pointArr["0"]);

        $pointname = $nameArr["1"];
        $longitude = $pointArr["2"] / 10000;
        $latitude = $pointArr["1"] / 10000;

        $kml_description = "Visits: " . $pointArr["3"] . "
Pages/Visit: "
. $pointArr["4"] . "
"
. $nameArr["0"] . " / " . $nameArr["1"];
        $kml_description = ".$kml_description."]]>";

        // — Create kml placemark
        $kml_output ="
                       
                                $pointname
                                $kml_description
                                #googelicon
                               
                                        $longitude,$latitude,0
                               

                       
"
;
        echo $kml_output;
}
// — Finnish off the kml
echo "";

?>
 

analytics2kml-starter.php


        header("content-type:application/vnd.google-earth.kml+xml");

        $kml_output=;
        $kml_output.=;

        $kml_output.=
               
                        Analytics 2 kml
                       
                                https://snowland.se/a2k/analytics2kml.php
                                never
                       

               
;
        $kml_output.=‘’;

        echo $kml_output;
?>