class TopPagesHandler(BaseHandler): @web.authenticated @cache(CACHE_EXPIRES) # set the cache expires @gen.coroutine def get(self): """ Returns posts that are most popular. Example: headers: [{'columnType': 'DIMENSION', 'dataType': 'STRING', 'name': 'ga:pagePath'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:pageviews'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:uniquePageviews'}, {'columnType': 'METRIC', 'dataType': 'TIME', 'name': 'ga:timeOnPage'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:bounces'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:entrances'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:exits'}], rows: [['/2015/07/08/a-deep-dive-into-angular-2-0/', '4327', '4043', '126847.0', '3741', '4012', '3993'], ['/', '1892', '1689', '3CACHE_EXPIRES65.0', '1095', '1590', '1191'], ['/2014/02/24/experiences-with-spring-boot/', '1166', '1133', '16328.0', '1097', '1129', '1127'], :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_pages(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 pages data') else: # formatting seconds to more human readable version and creating urls list urls = [] for row in data: m, s = divmod(int(float(row[3])), 60) h, m = divmod(m, 60) row[3] = "%d:%02d:%02d" % (h, m, s) urls.append(self.settings['website'] + row[0]) # getting social networks shares for our precious blog facebook_shares, twitter_shares, linkedin_shares = yield [get_facebook_results(urls), get_twitter_results(urls), get_linkedin_results(urls)] # updating list with data for idx, row in enumerate(data): row.append(facebook_shares[idx]) row.append(twitter_shares[idx]) row.append(linkedin_shares[idx]) headers = ['Path', 'Page views', 'Unique views', 'Avg. time on page', 'Bounces', 'Ent.', 'Exits', '<i class="fa fa-fw fa-facebook-official"></i>', '<i class="fa fa-fw fa-twitter"></i>', '<i class="fa fa-fw fa-linkedin"></i>'] table_title = 'Which posts are most popular?' return self.render('webhandler/data_table.html', data=data, table_title=table_title, headers=headers, website=self.settings['website']) except Exception as ex: self.set_status(403) return self.render_string('error.html', error=ex)
class TopPagesHandler(BaseHandler): @web.authenticated @cache(CACHE_EXPIRES) # set the cache expires @gen.coroutine def get(self): """ Returns posts that are most popular. Example: headers: [{'columnType': 'DIMENSION', 'dataType': 'STRING', 'name': 'ga:pagePath'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:pageviews'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:uniquePageviews'}, {'columnType': 'METRIC', 'dataType': 'TIME', 'name': 'ga:timeOnPage'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:bounces'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:entrances'}, {'columnType': 'METRIC', 'dataType': 'INTEGER', 'name': 'ga:exits'}], rows: [['/2015/07/08/a-deep-dive-into-angular-2-0/', '4327', '4043', '126847.0', '3741', '4012', '3993'], ['/', '1892', '1689', '3CACHE_EXPIRES65.0', '1095', '1590', '1191'], ['/2014/02/24/experiences-with-spring-boot/', '1166', '1133', '16328.0', '1097', '1129', '1127'], :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_pages(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 pages data') else: # formatting seconds to more human readable version and creating urls list urls = [] for row in data: m, s = divmod(int(float(row[3])), 60) h, m = divmod(m, 60) row[3] = "%d:%02d:%02d" % (h, m, s) urls.append(self.settings['website'] + row[0]) # getting social networks shares for our precious blog facebook_shares, twitter_shares, linkedin_shares = yield [get_facebook_results(urls), get_twitter_results(urls), get_linkedin_results(urls)] # updating list with data for idx, row in enumerate(data): row.append(facebook_shares[idx]) row.append(twitter_shares[idx]) row.append(linkedin_shares[idx]) headers = ['Path', 'Page views', 'Unique views', 'Avg. time on page', 'Bounces', 'Ent.', 'Exits', '<i class="fa fa-fw fa-facebook-official"></i>', '<i class="fa fa-fw fa-twitter"></i>', '<i class="fa fa-fw fa-linkedin"></i>'] table_title = 'Which posts are most popular?' return self.render('webhandler/data_table.html', data=data, table_title=table_title, headers=headers, website=self.settings['website']) except Exception as ex: self.set_status(403) return self.render('error.html', error=ex)