自制谷歌分析 [英] Homemade Google Analytics

查看:205
本文介绍了自制谷歌分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于谷歌分析可以提高许多隐私问题,我实现了一个事件的记录。

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.

有关当下的想法是记录器实例化为一个应用服务类,并使用这些元素的onCreate 的onDestroy 来安全地处理日志文件。

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.

解决的办法很简单:


  1. 开启文件

  2. 产生一个事件,每次追加到它

  3. 当一个 MAX_NUM_LINES 到达,发送日志到服务器(可能我会压缩我生成文本文件)

  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)

我不知道是否有什么已经出炉的还有在野外我不知道,你可能知道(像 ACRA )。
每一个贡献将是AP preciated。

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.

推荐答案

下面我的实现。
然而任何更好的版本是pciated多少AP $ P $。
霍芬海姆OBJET是只是我随着时间的经理使用静态类。

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

使用的code和,只要你重新发布改进/编辑修改。

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
    }
    }
}

这篇关于自制谷歌分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆