After I posted my first try of using Google Analytics Data Export API, I realized that I didn’t need to send two requests for calculating visits change, one is enough. Moreover, I could also make a chart.
=== General ===
125 | #
| # ## ## ####
| # ### ## ## #####
| ## ### ## # ###### ## # #### #####
|#### # # # # # ##### ############ ##### ###### ######
|#### ##### # ## ##########################################
|########### ###############################################
|########### ################################################
|############################################################
|############################################################
|############################################################
0 +------------------------------------------------------------
116 visits ( -7.20%)
Average time on site: 122.655172414 seconds ( 55.75%)</pre>
I wonder if there is a popular Python library or common CLI tool to make an ASCII chart.
DAYS = 60
date = (dt.datetime.now() - dt.timedelta(days=1)).strftime('%Y-%m-%d')
date_start = (dt.datetime.now() - dt.timedelta(days=DAYS)).strftime('%Y-%m-%d')
# General
###########
data_query = gdata.analytics.client.DataFeedQuery({
'ids': table_id,
'start-date': date_start,
'end-date': date,
'dimensions': 'ga:date',
'sort': 'ga:date',
'metrics': 'ga:visits,ga:avgTimeOnSite'})
feed = my_client.GetDataFeed(data_query)
visits = [int(entry.metric[0].value) for entry in feed.entry]
max_visits = max(visits)
print '=== General ==='
CHART_HEIGHT = 10
VISIT_WIDTH = len(str(max_visits))
for y in range(CHART_HEIGHT, -1, -1):
if y == CHART_HEIGHT:
sys.stdout.write('%d |' % max_visits)
else:
sys.stdout.write('%s |' % (' '*VISIT_WIDTH))
for x in range(-DAYS, 0):
vst = visits[x]
# vst / max_visits >= y / CHART_HEIGHT
if vst * CHART_HEIGHT >= y * max_visits:
sys.stdout.write('#')
else:
sys.stdout.write(' ')
sys.stdout.write('\n')
sys.stdout.flush()
print '%s0 +%s' % (' '*(VISIT_WIDTH-1), '-'*DAYS)
visits_change = 100.0 * (visits[-1] - visits[-2]) / visits[-2]
avg_time = float(feed.entry[-1].metric[1].value)
avg_time_before = float(feed.entry[-2].metric[1].value)
avg_time_change = 100.0 * (avg_time - avg_time_before) / avg_time_before
print ' %s visits (%7.2f%%)' % (visits[-1], visits_change)
print ' Average time on site: %s seconds (%7.2f%%)' % (avg_time, avg_time_change)
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.