// analytics2kml.cpp is copyright (c) 2006 by A. Jacob Cord, All rights reserved. /* * analytics2kml is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * analytics2kml is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* * This program is also comment-ware. If you like it, stop by my blog and leave * me a note! http://bogomip.net/blog/google-analytics-and-google-earth/ */ #include #include #include void help(); int main(int argc, char *argv[]) { if(argc != 2) { help(); return 0; } std::ifstream fin; fin.open(argv[1]); if(fin.fail()) { std::cerr << "Couldn't open " << argv[1] << " for processing." << std::endl; return 1; } int i = 0; std::string s; // output KML header std::cout << "" << std::endl; std::cout << "" << std::endl; std::cout << "\t" << std::endl; while(!std::getline(fin, s).eof()) { if(s.empty() || s.substr(0,1) == "#" || s.substr(0,1) == "(") { // skip blank lines, comments, and (not set) tokens continue; } std::string country, city; float lat = 0.0, lon = 0.0; int visits = 0; std::string::size_type t, pos = 0; t = s.find("-", pos); if(t == std::string::npos) { std::cerr << "Couldn't find a dash character!" << std::endl; continue; } country = s.substr(pos, t); pos = t + 1; t = s.find("|", pos); if(t == std::string::npos) { std::cerr << "Couldn't find a pipe character!" << std::cerr; continue; } city = s.substr(pos, t-pos); pos = t + 1; t = s.find("|", pos); if(t == std::string::npos) { std::cerr << "Couldn't find second pipe character!" << std::endl; continue; } lat = atof(s.substr(pos, t-pos).c_str()); pos = t + 1; t = s.find("\t", pos); if(t == std::string::npos) { std::cerr << "Couldn't find a tab character!" << std::endl; continue; } lon = atof(s.substr(pos, t-pos).c_str()); pos = t + 1; t = s.find("\t", pos); if(t == std::string::npos) { std::cerr << "Couldn't find second tab character!" << std::endl; continue; } visits = atoi(s.substr(pos, t-pos).c_str()); std::cout << "\t\t" << std::endl; ++i; std::cout << "\t\t\t" << city << " (" << visits << " visit"; if(visits != 1) { std::cout << "s"; } std::cout << ")" << std::endl; std::cout << "\t\t\t" << std::endl; std::cout << "\t\t\t\t" << lon / 10000 << "," << lat / 10000 << ",0" << std::endl; std::cout << "\t\t\t" << std::endl << "\t\t" << std::endl; } std::cout << "\t" << std::endl << "" << std::endl; return 0; } void help() { std::cout << "Usage: analytics2kml " << std::endl; std::cout << "\tThis program translates Google Analytics tab-delimited data and " << std::endl; std::cout << "outputs KML data for Google Earth on std::out. Use redirection to send " << std::endl; std::cout << "it to a file. For example: google-kml report.txt > report.kml" << std::endl; std::cout << "\n\nThis program is also comment-ware. If you like it, stop by my blog and leave" << std::endl; std::cout << "me a note! http://bogomip.net/blog/google-analytics-and-google-earth/" << std::endl; }