Search code examples
androidevent-log

Homemade Google Analytics


Since Google analytics can raise many privacy concerns, I implemented an events logger.

My first idea is to track user's generated events into a logfile and then send them back to the server that will perform the analysis of data for the System Administrator and Application Engineers.

For the moment the idea is to instantiate the Logger into an Application or a Service class and use those elements onCreate and onDestroy to safely handle the LogFile.

The solution is quite simple:

  1. Open file
  2. Append to it every time an event is generated
  3. Once the a MAX_NUM_LINES is reached, send the log to the server (possibly I'll zip the text file I am generating)

I wonder if there's anything already baked there in the wild I am unaware of that you might know (something like ACRA). Every contribution will be appreciated.


Solution

  • Here my implementation. However any better version is much appreciated. The TSG objet is just a static class that I use as time manager.

    Use the code and improve it as long as you repost / edit the modifications.

    public class Logger {
        private BufferedWriter logFile;
        private String nameFile;
        public int fileLines;
        private File fTemp;
        private timeStampGenerator TSG;
        private int LOG_LINES_LIMIT = 100;
        private Object mutex;
    
        public enum EventType {
            BUTTON_PRESSED, 
            PAGE_VIEWED, 
            LOADED_ACTIVITY,  
            GENERIC_EVENT
        }
    
        public Logger (String fileName) throws IOException {
            nameFile = fileName;
    
            createLogFile();
    
            fileLines = countLines();
    
            TSG = new timeStampGenerator();
    
            // This is our mutex to access to the file
            mutex = new Object();
        }
    
    
        public void createLogFile() throws IOException{
            fTemp = new File (nameFile);
    
            if (!fTemp.exists()) {
                fTemp.createNewFile();
            }
    
            logFile = new BufferedWriter(new FileWriter(nameFile, true));
    
        }
    
        public void LogEvent(EventType event, String comment, String value) {
    
            String line = "";
            line += TSG.getTimestampMillis();
            line += ",";
            line += event.name();
            line += ",";
    
            if (comment != "") {
                line += comment.replaceAll(",", ";");
            } else {
                line += " ";
            }
    
            line += ",";
    
            if (value != "") {
                line += value.replaceAll(",", ";");
            } else {
                line += " ";
            }
    
            line += "\n";
    
            synchronized (mutex) {
                try {
                    logFile.append(line);
                } catch (IOException e) {
                    // Do wathever you want here
                }
                fileLines++;
            }
        }
    
    
        public int countLines() //throws IOException 
        {
            InputStream is;
            try {
                is = new BufferedInputStream(new FileInputStream(nameFile));
            } catch (FileNotFoundException e1) {
                //let's consider it an empty file
                return 0;
            }
    
    
            int count = 0;
            boolean empty = true;
    
    
            try {
                int readChars = 0;
    
                byte[] c = new byte[1024];
    
                while ((readChars = is.read(c)) != -1) {
                    empty = false;
                    for (int i = 0; i < readChars; ++i) {
                        if (c[i] == '\n')
                            ++count;
                    }
                }
            } catch(IOException e) {
                // Do wathever you want here
            }
    
    
            try {
                is.close();
            } catch (IOException e) {
                // Do wathever you want here
            }
    
            return (count == 0 && !empty) ? 1 : count;
        }
    
    
        public boolean isLimitReached() {
    
            return (fileLines >= LOG_LINES_LIMIT);
    
        }
    
    
        public void close () {
            flush();
            try {
                logFile.close();
            } catch (IOException e) {
                // Do wathever you want here
            }
        }
    
    
    
        /** 
         * clear the content of the file
         */
        public void clearFile() {
            synchronized (mutex) {
                if ( fTemp.delete() ) {
                    try {
                        createLogFile();
                    } catch (IOException e1) {
                        // Do wathever you want here
                    }
                }
    
            }
        }
    
    
        /**
         *  Get the full content of the file
         * @return the content
         */
        public String getContent() {
    
            StringBuffer fileData = new StringBuffer();
    
            synchronized (mutex) {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader( nameFile ));
                    char[] buf = new char[1024];
                    int numRead = 0;
                    while ((numRead = reader.read(buf)) != -1) {
                        String readData = String.valueOf(buf, 0, numRead);
                        fileData.append(readData);
                    }
                    reader.close();
                } catch (IOException e) {
                    // Do wathever you want here
                }
            }
    
            return fileData.toString();
        }
    
        public void flush() {
            try {
                logFile.flush();
            } catch (IOException e) {
                // Do wathever you want here
        }
        }
    }