Search in sources :

Example 1 with GaData

use of com.google.api.services.analytics.model.GaData 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 GaData

use of com.google.api.services.analytics.model.GaData in project pentaho-kettle by pentaho.

the class GaInputStepDialogTest method setup.

@Before
public void setup() throws IOException, KettlePluginException {
    PluginRegistry.addPluginType(ValueMetaPluginType.getInstance());
    PluginRegistry.init();
    GaData gaData = new GaData();
    headers = new ArrayList<>();
    headers.add(createColumnHeader("DIMENSION", "ga:date", null));
    headers.add(createColumnHeader("DIMENSION", "ga:daysSinceLastVisit", null));
    headers.add(createColumnHeader("DIMENSION", "ga:visitLength", null));
    headers.add(createColumnHeader("DIMENSION", "ga:visitCount", null));
    headers.add(createColumnHeader("DIMENSION", "ga:latitude", null));
    headers.add(createColumnHeader("DIMENSION", "ga:longitude", null));
    headers.add(createColumnHeader("DIMENSION", "ga:other", null));
    headers.add(createColumnHeader("METRIC", "currency", "currency"));
    headers.add(createColumnHeader("METRIC", "float", "float"));
    headers.add(createColumnHeader("METRIC", "percent", "percent"));
    headers.add(createColumnHeader("METRIC", "us_currency", "us_currency"));
    headers.add(createColumnHeader("METRIC", "time", "time"));
    headers.add(createColumnHeader("METRIC", "integer", "integer"));
    headers.add(createColumnHeader("METRIC", "other", "other"));
    gaData.setColumnHeaders(headers);
    gaData.setProfileInfo(new GaData.ProfileInfo());
    List<List<String>> data = new ArrayList<>();
    data.add(new ArrayList<String>());
    gaData.setRows(data);
    doReturn(gaData).when(query).execute();
    doReturn(tableItem).when(table).getItem(anyInt());
    tableView.table = table;
    doReturn(tableView).when(dialog).getTableView();
    doCallRealMethod().when(dialog).getFields();
    doReturn(query).when(dialog).getPreviewQuery();
    doReturn(mock(GaInputStepMeta.class)).when(dialog).getInput();
}
Also used : GaInputStepMeta(org.pentaho.di.trans.steps.googleanalytics.GaInputStepMeta) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GaData(com.google.api.services.analytics.model.GaData) Before(org.junit.Before)

Example 3 with GaData

use of com.google.api.services.analytics.model.GaData 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)

Example 4 with GaData

use of com.google.api.services.analytics.model.GaData in project pentaho-kettle by pentaho.

the class GaInputStepDialog method getFields.

// Visible for testing
void getFields() {
    Analytics.Data.Ga.Get query = getPreviewQuery();
    if (query == null) {
        return;
    }
    query.setMaxResults(1);
    try {
        GaData dataFeed = query.execute();
        if (dataFeed == null || dataFeed.getRows() == null || dataFeed.getRows().size() < 1) {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setText("Query yields empty feed");
            mb.setMessage("The feed did not give any results. Please specify a query that returns data.");
            mb.open();
            return;
        }
        int i = 0;
        List<GaData.ColumnHeaders> colHeaders = dataFeed.getColumnHeaders();
        getTableView().table.setItemCount(colHeaders.size() + dataFeed.getProfileInfo().size());
        for (GaData.ColumnHeaders colHeader : colHeaders) {
            String name = colHeader.getName();
            String dataType = colHeader.getDataType();
            String columnType = colHeader.getColumnType();
            TableItem item = getTableView().table.getItem(i);
            if (columnType.equals("DIMENSION")) {
                item.setText(1, GaInputStepMeta.FIELD_TYPE_DIMENSION);
                item.setText(2, name);
                item.setText(3, name);
                // recognize date dimension
                if (name.equalsIgnoreCase("ga:date")) {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_DATE));
                    item.setText(5, "yyyyMMdd");
                } else if (name.equalsIgnoreCase("ga:daysSinceLastVisit") || name.equalsIgnoreCase("ga:visitLength") || name.equalsIgnoreCase("ga:visitCount")) {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_INTEGER));
                    item.setText(5, "#;-#");
                } else if (name.equalsIgnoreCase("ga:latitude") || name.equalsIgnoreCase("ga:longitude")) {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_NUMBER));
                    item.setText(5, "#.#;-#.#");
                } else {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
                    item.setText(5, "");
                }
                i++;
            } else if (columnType.equals("METRIC")) {
                item.setText(1, GaInputStepMeta.FIELD_TYPE_METRIC);
                item.setText(2, name);
                item.setText(3, name);
                // depending on type
                if (dataType.compareToIgnoreCase("currency") == 0 || dataType.compareToIgnoreCase("float") == 0 || dataType.compareToIgnoreCase("percent") == 0 || dataType.compareToIgnoreCase("us_currency") == 0) {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_NUMBER));
                    item.setText(5, "#.#;-#.#");
                } else if (dataType.compareToIgnoreCase("time") == 0 || dataType.compareToIgnoreCase("integer") == 0) {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_INTEGER));
                    item.setText(5, "#;-#");
                } else {
                    item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
                    item.setText(5, "");
                }
                i++;
            }
        }
        // Fill ds property and ds fields
        TableItem item = getTableView().table.getItem(i);
        item.setText(1, GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_PROPERTY);
        item.setText(2, GaInputStepMeta.PROPERTY_DATA_SOURCE_PROFILE_ID);
        item.setText(3, GaInputStepMeta.PROPERTY_DATA_SOURCE_PROFILE_ID);
        item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        item.setText(5, "");
        i++;
        item = getTableView().table.getItem(i);
        item.setText(1, GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_PROPERTY);
        item.setText(2, GaInputStepMeta.PROPERTY_DATA_SOURCE_WEBPROP_ID);
        item.setText(3, GaInputStepMeta.PROPERTY_DATA_SOURCE_WEBPROP_ID);
        item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        item.setText(5, "");
        i++;
        item = getTableView().table.getItem(i);
        item.setText(1, GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_PROPERTY);
        item.setText(2, GaInputStepMeta.PROPERTY_DATA_SOURCE_ACCOUNT_NAME);
        item.setText(3, GaInputStepMeta.PROPERTY_DATA_SOURCE_ACCOUNT_NAME);
        item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        item.setText(5, "");
        i++;
        item = getTableView().table.getItem(i);
        item.setText(1, GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_FIELD);
        item.setText(2, GaInputStepMeta.FIELD_DATA_SOURCE_TABLE_ID);
        item.setText(3, GaInputStepMeta.FIELD_DATA_SOURCE_TABLE_ID);
        item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        item.setText(5, "");
        i++;
        item = getTableView().table.getItem(i);
        item.setText(1, GaInputStepMeta.FIELD_TYPE_DATA_SOURCE_FIELD);
        item.setText(2, GaInputStepMeta.FIELD_DATA_SOURCE_TABLE_NAME);
        item.setText(3, GaInputStepMeta.FIELD_DATA_SOURCE_TABLE_NAME);
        item.setText(4, ValueMetaBase.getTypeDesc(ValueMetaInterface.TYPE_STRING));
        item.setText(5, "");
        getTableView().removeEmptyRows();
        getTableView().setRowNums();
        getTableView().optWidth(true);
        getInput().setChanged();
    } catch (IOException ioe) {
        Exception exceptionToDisplay = ioe;
        // Try to display something more user friendly than plain JSON
        if (ioe instanceof GoogleJsonResponseException) {
            GoogleJsonResponseException gjre = (GoogleJsonResponseException) ioe;
            if (gjre.getDetails() != null && gjre.getDetails().getMessage() != null) {
                exceptionToDisplay = new IOException(gjre.getDetails().getMessage(), gjre);
            }
        }
        new ErrorDialog(shell, BaseMessages.getString(PKG, "GoogleAnalyticsDialog.RequestError.DialogTitle"), BaseMessages.getString(PKG, "GoogleAnalyticsDialog.RequestError.DialogMessage"), exceptionToDisplay);
    }
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) IOException(java.io.IOException) Analytics(com.google.api.services.analytics.Analytics) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IOException(java.io.IOException) MessageBox(org.eclipse.swt.widgets.MessageBox) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) GaData(com.google.api.services.analytics.model.GaData)

Aggregations

GaData (com.google.api.services.analytics.model.GaData)4 IOException (java.io.IOException)3 Get (com.google.api.services.analytics.Analytics.Data.Ga.Get)2 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Analytics (com.google.api.services.analytics.Analytics)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 TableItem (org.eclipse.swt.widgets.TableItem)1 Before (org.junit.Before)1 GaInputStepMeta (org.pentaho.di.trans.steps.googleanalytics.GaInputStepMeta)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1