Search in sources :

Example 1 with Get

use of com.google.api.services.analytics.Analytics.Data.Ga.Get in project pratilipi by Pratilipi.

the class GoogleAnalyticsApi method getPageViews.

public static Map<String, Integer> getPageViews(String date) throws UnexpectedServerException {
    Map<String, Integer> uriViewsMap = new HashMap<String, Integer>();
    try {
        while (true) {
            Get apiQuery = getAnalytics().data().ga().get(// Table Id.
            "ga:96325104", // Start Date YYYY-MM-DD
            date, // End Date YYYY-MM-DD
            date, // Metrics.
            "ga:pageviews").setDimensions("ga:pagePath").setStartIndex(uriViewsMap.size() + 1).setMaxResults(10000);
            GaData gaData = apiQuery.execute();
            if (gaData.getRows() != null)
                for (List<String> row : gaData.getRows()) uriViewsMap.put(row.get(0), Integer.parseInt(row.get(1)));
            if (uriViewsMap.size() == gaData.getTotalResults())
                break;
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to fetch data from Google Analytics.", e);
        throw new UnexpectedServerException();
    }
    return uriViewsMap;
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) Get(com.google.api.services.analytics.Analytics.Data.Ga.Get) List(java.util.List) LinkedList(java.util.LinkedList) GaData(com.google.api.services.analytics.model.GaData) IOException(java.io.IOException)

Example 2 with Get

use of com.google.api.services.analytics.Analytics.Data.Ga.Get in project pratilipi by Pratilipi.

the class GoogleAnalyticsApi method getPratilipiReadCount.

public static Map<Long, Long> getPratilipiReadCount(List<Long> pratilipiIdList) throws UnexpectedServerException {
    int idsPerRequest = 80;
    Map<Long, Long> idCountMap = new HashMap<Long, Long>();
    for (int i = 0; i < pratilipiIdList.size(); i = i + idsPerRequest) {
        String filters = "";
        for (int j = 0; i + j < pratilipiIdList.size() && j < idsPerRequest; j++) filters = filters + "ga:pagePath=~^/read\\?id=" + pratilipiIdList.get(i + j) + ".*,";
        filters = filters.substring(0, filters.length() - 1);
        try {
            Get apiQuery = getAnalytics().data().ga().get(// Table Id.
            "ga:89762686", // Start Date.
            "2015-01-01", // End Date.
            "today", // Metrics.
            "ga:pageviews").setDimensions("ga:pagePath").setFilters(filters);
            GaData gaData = apiQuery.execute();
            if (gaData.getRows() != null) {
                for (List<String> row : gaData.getRows()) {
                    String pagePath = row.get(0);
                    if (pagePath.indexOf('&') != -1)
                        pagePath = pagePath.substring(0, pagePath.indexOf('&'));
                    Long pratilipiId = Long.parseLong(pagePath.substring(pagePath.indexOf('=') + 1));
                    long readCount = Long.parseLong(row.get(1));
                    if (idCountMap.containsKey(pratilipiId))
                        idCountMap.put(pratilipiId, readCount + idCountMap.get(pratilipiId));
                    else
                        idCountMap.put(pratilipiId, readCount);
                }
            }
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to fetch data from Google Analytics.", e);
            throw new UnexpectedServerException();
        }
    }
    return idCountMap;
}
Also used : UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) HashMap(java.util.HashMap) Get(com.google.api.services.analytics.Analytics.Data.Ga.Get) GaData(com.google.api.services.analytics.model.GaData) IOException(java.io.IOException)

Aggregations

Get (com.google.api.services.analytics.Analytics.Data.Ga.Get)2 GaData (com.google.api.services.analytics.model.GaData)2 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1