def handle_overview_question(message): message.reply( "Let me check what's been happening. This may take a second.") seven_days_ago = datetime.datetime.now() - datetime.timedelta(days=7) stories = Story.select().where(Story.tracking_started > seven_days_ago) slugs = Set() for story in stories: print story.name story_slugs = story.slug.split(',') for slug in story_slugs: slugs.add(slug) total_users = analytics.get_user_data(start_date='7daysAgo') total_users = int(total_users['rows'][0][0]) total_users = "{:,}".format(total_users) median = analytics.get_linger_rate(start_date='7daysAgo') linger_rows = analytics.get_linger_rows(start_date='7daysAgo') linger_histogram_url = ChartTools.linger_histogram_link( linger_rows, median) attachments = [{ "fallback": "linger update", "color": "#eeeeee", "title": "Time spent on graphics over the last week", "image_url": linger_histogram_url }] slackTools.send_message( message.body['channel'], "In the past 7 days, I've tracked %s stories and %s graphics." % (len(stories), len(slugs))) slackTools.send_message( message.body['channel'], "%s people looked at graphics on those stories. Here's how much time they spent:" % total_users, attachments, unfurl_links=False) fields = [] for story in stories: print "Adding %s" % story.name fields.append({ "title": story.name.strip(), "value": '<' + story.url + '|' + story.slug.strip() + '>', "short": True }) attachments = [{ "fallback": "linger update", "color": "#eeeeee", # "title": "What we have done", "fields": fields }] slackTools.send_message(message.body['channel'], "Here's everything:", attachments, unfurl_links=False)
def get_update_message(self, story): , data, time_bucket channel = story.channel() message = ("%s hours in and here's what I know about the graphics on _%s_:") % ( self.hours_since(story.date), story.name, ) # remove `Baseline` from data data = data[1:] histogram_url = ChartTools.scroll_histogram_link(data, labels=( '100%', '75%', '50%', '25%',)) if story.screenshot: histogram_url = ChartTools.add_screenshot_to_chart( story.screenshot, histogram_url) fields = [] for row in data: fields.append({ "title": row[0], "value": row[2], "short": True, }) attachments = [ { "fallback": story.name + " update", "color": "#eeeeee", "title": story.name, "title_link": story.url, "fields": fields, "image_url": histogram_url, } ] self.slack.chat.post_message( channel, message, as_user=True, parse='full', attachments=attachments)
def handle_scroll_slug_question(message): m = re.search(SCROLL_RATE_REGEX, message.body['text']) if not m: return slug = m.group(1) if slug: stories = Story.select().where(Story.slug.contains(slug)) rows = analytics.get_depth_rate(slug) if rows: reply = u"Here's what I know about `%s`." % slug reply += '\n\nThis graphic appears in %s %s:' % ( inflector.number_to_words( len(stories)), inflector.plural('story', len(stories))) for story in stories: reply += '\n' + '*<%s|%s>*' % (story.url, story.name.strip()) histogram_url = ChartTools.scroll_histogram_link(rows) if story.screenshot: histogram_url = ChartTools.add_screenshot_to_chart( story.screenshot, histogram_url) attachments = [{ "fallback": slug + " update", "color": "#eeeeee", "title": slug, "image_url": histogram_url }] slackTools.send_message(message.body['channel'], reply, attachments, unfurl_links=False) else: message.reply("I wasn't able to find scroll data for %s" % slug)
def get_slug_message(self, slug, story=None): # Try to match the story to a slug to accurately get a team # The Google Analytics property ID comes from the team config # We use the default team if none is found stories = Story.select().where(Story.slug.contains(slug)) team = self.config.get_team_for_stories(stories) params = self.get_slug_query_params(team=team, slug=slug) data = GoogleAnalytics.query_ga(params) if not data.get('rows'): logger.info('No rows found for slug %s' % slug) return # Clean up the data clean_data = self.clean_data(data.get('rows')) total_people = self.get_total_people(clean_data) friendly_people = "{:,}".format(total_people) # Comma-separated #s median = self.get_median(clean_data) # Set up the chart scroll_histogram_url = self.get_chart(clean_data) if story: scroll_histogram_url = ChartTools.add_screenshot_to_chart(story, scroll_histogram_url) # TODO: Not confident in median calculations so far # text = "*%s people* got a median of *%s percent* down the page." % (friendly_people, median) text = '' attachments = [{ "fallback": slug + " update", "color": "#eeeeee", "title": "How far down did people scroll?", "image_url": scroll_histogram_url }] return { 'text': text, 'attachments': attachments }
def handle_slug_question(message): m = re.search(LINGER_RATE_REGEX, message.body['text']) if not m: return slug = m.group(1) if slug: median = analytics.get_linger_rate(slug) stories = Story.select().where(Story.slug.contains(slug)) message.reply("Ok! I'm looking up %s. This may take a second." % slug) if median: people = "{:,}".format(median['total_people']) time_text = TimeTools.humanist_time_bucket(median) reply = u"*%s* people spent a median *%s* on `%s`." % ( people, time_text, slug) # List the stories this slug appears on reply += '\n\nThis graphic appears in %s %s:' % ( inflector.number_to_words( len(stories)), inflector.plural('story', len(stories))) for story in stories: reply += '\n' + '*<%s|%s>*' % (story.url, story.name.strip()) # Get linger rate data linger_rows = analytics.get_linger_rows(slug) linger_histogram_url = ChartTools.linger_histogram_link( linger_rows, median) all_graphics_rows = analytics.get_linger_rows() all_graphics_median = analytics.get_linger_rate() all_histogram = ChartTools.linger_histogram_link( all_graphics_rows, all_graphics_median) attachments = [{ "fallback": slug + " update", "color": "#eeeeee", "title": slug, "image_url": linger_histogram_url }, { "fallback": slug + " update", "color": "#eeeeee", "title": "How all graphics performed", "image_url": all_histogram }] # Get scroll data, if any. scroll_depth_rows = analytics.get_depth_rate(slug) if scroll_depth_rows: scroll_histogram_url = ChartTools.scroll_histogram_link( scroll_depth_rows) if stories[0].screenshot: scroll_histogram_url = ChartTools.add_screenshot_to_chart( stories[0].screenshot, scroll_histogram_url) attachments.append({ "fallback": slug + " update", "color": "#eeeeee", "title": "How far down did people scroll?", "image_url": scroll_histogram_url }) slackTools.send_message(message.body['channel'], reply, attachments, unfurl_links=False) else: message.reply("I wasn't able to figure out the linger rate of %s" % slug)