class TopKeywordsHandler(BaseHandler):
    @web.authenticated
    @cache(CACHE_EXPIRES)  # set the cache expires
    @unblock
    def get(self):
        """
        Returns a list of top search terms that have been used to find you

        example:
        headers:
        [{'columnType': 'DIMENSION',
                    'dataType': 'STRING',
                    'name': 'ga:keyword'},
                   {'columnType': 'METRIC',
                    'dataType': 'INTEGER',
                    'name': 'ga:sessions'}],
        rows:
        [['linkplug-angularjs-job-board-contractor-listings', '302'],
          ['spring with cassandra', '13'],
          ['opencredo', '5'],
          ['https://www.opencredo.com/2015/07/08/a-deep-dive-into-angular-2-0/',
           '4'],
          ['0_4bba7e0c9e-e432a867c8-344988337', '3'],

        :return:
        """
        try:
            service_account = self.settings['service_account_email']
            self.service = GAcess(service_account_email=service_account,
                                  key_file_location=self.settings['key_file_location'])

            query_result = self.service.get_top_keywords(profile_id=self.settings['ga_profile_id'],
                                                         days=self.settings['start_days_ago'])
            try:
                data = query_result['rows']
            except KeyError:
                self.set_status(400, reason='Failed to fetch top keywords data')
            else:
                table_title = 'What keywords were used to find us?'
                headers = ['Keyword', 'Sessions']
                return self.render_string('webhandler/data_table.html',
                                          data=data,
                                          table_title=table_title,
                                          headers=headers)
        except Exception as ex:
            self.set_status(403)
            return self.render_string('error.html',
                                      error=ex)
class TopKeywordsHandler(BaseHandler):
    @web.authenticated
    @cache(CACHE_EXPIRES)  # set the cache expires
    @unblock
    def get(self):
        """
        Returns a list of top search terms that have been used to find you

        example:
        headers:
        [{'columnType': 'DIMENSION',
                    'dataType': 'STRING',
                    'name': 'ga:keyword'},
                   {'columnType': 'METRIC',
                    'dataType': 'INTEGER',
                    'name': 'ga:sessions'}],
        rows:
        [['linkplug-angularjs-job-board-contractor-listings', '302'],
          ['spring with cassandra', '13'],
          ['opencredo', '5'],
          ['https://www.opencredo.com/2015/07/08/a-deep-dive-into-angular-2-0/',
           '4'],
          ['0_4bba7e0c9e-e432a867c8-344988337', '3'],

        :return:
        """
        try:
            service_account = self.settings['service_account_email']
            self.service = GAcess(service_account_email=service_account,
                                  key_file_location=self.settings['key_file_location'])

            query_result = self.service.get_top_keywords(profile_id=self.settings['ga_profile_id'],
                                                         days=self.settings['start_days_ago'])
            try:
                data = query_result['rows']
            except KeyError:
                self.set_status(400, reason='Failed to fetch top keywords data')
            else:
                table_title = 'What keywords were used to find us?'
                headers = ['Keyword', 'Sessions']
                return self.render_string('webhandler/data_table.html',
                                          data=data,
                                          table_title=table_title,
                                          headers=headers)
        except Exception as ex:
            self.set_status(403)
            return self.render_string('error.html',
                                      error=ex)