function getTopFBPosts($account, $count = 10) { $start = tryGET('start'); $end = tryGet('end'); $token = getFBToken(); if (!isset($token[0])) { DoNotCache("Facebook: Authentication failed"); return null; } //No token $url = "https://graph.facebook.com/{$account}/posts"; $params = array($token[0] => $token[1], "limit" => 100, "fields" => "likes.limit(1).summary(true),shares,actions", "since" => $start, "until" => $end); $result = getAPI($url, $params); if (!isset($result->data)) { return array(); } $rdata = $result->data; usort($rdata, 'fbSort'); $posts = array(); foreach ($rdata as $key => $p) { if ($key < $count) { array_push($posts, $p); } else { break; } } return $posts; }
function topTweets($user = null, $count = 20) { if (!isset($user)) { $user = tryGET('user'); } $start = tryGET('start'); $end = tryGET('end'); $tc = tryGET('count'); if (isset($tc)) { $count = $tc; } //print "COUNT|$count"; $tweets = getTweetsByDate($user, $start, $end); if (isset($tweets)) { usort($tweets, "tweetSort"); $tweets = array_splice($tweets, 0, $count); return $tweets; } }
function getTopIGMedia($userID, $count = 10) { $start = tryGET('start'); $end = tryGET('end'); $url = "https://api.instagram.com/v1/users/{$userID}/media/recent"; $params = array("min_timestamp" => $start, "max_timestamp" => $end, "count" => "200", "client_id" => getIGClientID()); $res = getAPI($url, $params); if (!isset($res->data)) { return DoNotCache(); } $media = $res->data; usort($media, "igSort"); $result = array(); foreach ($media as $key => $p) { if ($key < $count) { array_push($result, $p); } else { break; } } return $result; }
function getTopIGMedia($userID, $count = 10) { $start = tryGET('start'); $end = tryGET('end'); $url = "https://api.instagram.com/v1/users/{$userID}/media/recent"; $params = array("min_timestamp" => $start, "max_timestamp" => $end, "count" => "200", "client_id" => getIGClientID()); $res = getAPI($url, $params); if (!isset($res->data)) { } return null; // return DoNotCache("Couldn't get Instagram Media: " . json_encode($res)); //TODO: Figure out how we can rework IG to work. $media = $res->data; usort($media, "igSort"); $result = array(); foreach ($media as $key => $p) { if ($key < $count) { array_push($result, $p); } else { break; } } return $result; }
function getMultiBadgeHTML($act) { $imports = $act["locations"]; $type = $act["type"]; $start = tryGET('start'); $end = tryGET('end'); $followers = 0; $hasFollowers = false; $change = 0; $hasChange = false; $acts = array(); foreach ($imports as $key => $imp) { $act = getImportAct($imp, $type); if (!isset($act)) { continue; } $acts[] = $act; //Followers $tFollow = getFollowers($act["id"], $end); if (isset($tFollow)) { $followers += $tFollow; $hasFollowers = true; } // Change in followers (delta) $tChange = getFollowerChange($act["id"], $start, $end); if (isset($tChange)) { $change += $tChange; $hasChange = true; } } if (!$hasFollowers) { $followers = '?'; } else { $followers = number_format($followers); } if (!$hasChange) { $change = '?'; $dirclass = ''; } else { if ($change > 0) { $dirclass = 'fa-chevron-up'; $change = number_format($change); } else { if ($change < 0) { $dirclass = 'fa-chevron-down'; $change *= -1; $change = number_format($change); } else { $dirclass = 'fa-minus'; $change = 'No Change'; } } } $toppages = getTopRefferalPagesByType($type); $topposts = getMultiTopPosts($acts, $type); $typeclass = str_replace(" ", "-", $type); switch ($type) { case 'twitter': $postname = "Tweet"; $faicon = "fa-twitter"; break; case 'facebook': $postname = "Post"; $faicon = "fa-facebook"; break; case 'instagram': $postname = "Image"; $faicon = "fa-instagram"; break; default: $postname = "Post"; $faicon = ""; } $html = "\n\n<!-- Start Social Badge -->\n\n"; $html .= "<div class=' col-md-4 col-xs-12'>\n"; $html .= "\t<div class='" . $typeclass . " social-pane panel'>\n"; $html .= "\t\t<div class='panel-heading'>\n"; $html .= "\t\t\t<h3 class='panel-title social-title'><i class='fa {$faicon}'></i> " . ucfirst($type) . "</h3>\n"; $html .= "\t\t</div>\n"; $html .= "\t\t<div class='social-body panel-body'>\n"; $html .= "\t\t\t<h4>Total Followers: <b>{$followers}</b></h4>\n"; $html .= "\t\t\t<h4>Change in Followers: <i class='fa {$dirclass}'></i> <b>{$change}</b></h4>\n"; if (count($toppages) > 0) { $html .= "\t\t\t<h4>Top Pages Visited From " . ucfirst($type) . "</h4>\n"; $html .= "\t\t\t<div class='social-urls'>\n"; $html .= "\t\t\t\t<ol>\n"; foreach ($toppages as $key => $u) { $url = $u['url']; $title = $u['title']; $html .= "\t\t\t\t\t<li><a target='_blank' href='//{$url}'>{$title}</a></li>\n"; } $html .= "\t\t\t\t</ol>\n"; $html .= "\t\t\t</div>\n"; /* $html .= "\t\t\t<h5>None :(</h5>\n"; */ } if (count($topposts) > 0) { $html .= "\t\t\t<h4>Top " . $postname . "s</h4>\n"; $html .= "\t\t\t<div class='social-urls'>\n"; $html .= "\t\t\t\t<ol>\n"; foreach ($topposts as $key => $p) { $url = $p["url"]; $user = $p['username']; $html .= "\t\t\t\t\t<li><a target='_blank' href='{$url}'>{$user}'s {$postname}</a></li>\n"; } $html .= "\t\t\t\t</ol>\n"; $html .= "\t\t\t</div>\n"; } $html .= "\t\t</div>\n"; $html .= "\t</div>\n"; $html .= "</div>\n"; $html .= "\n\n<!-- End Social Badge -->\n\n"; return $html; }
function getTopDeviations($account = null, $count = null) { $pcount = $count; $qcount = tryGET('count'); $vcount = 10; if (isset($pcount)) { $vcount = $pcount; } else { if (isset($qcount)) { $vcount = $qcount; } } if ($vcount > 100) { $vcount = 100; } $settings = getSettings(); if (!isset($account)) { $account = $settings["Account"]; } $start = tryGET('start'); $end = tryGET('end'); if (!isset($start) || !isset($end)) { return null; } $ndays = getDays($start, $end); $start = GoogleDate($start); $end = GoogleDate($end); $analytics = getAnalytics(); $filter = ""; //More than 1 pageview an hour to cut down on outliers and processing $dims = "ga:hostname,ga:pagePath,ga:date"; $metric = "ga:pageviews"; $sort = "-ga:pageviews"; $count = 10000; //max $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter); if (isset($data->ga_error)) { return DoNotCache(); } $data = $data->getRows(); $values = array(); $path = ''; $tvals = array(); foreach ($data as $key => $row) { if (!isset($values[$row[0] . $row[1]])) { $values[$row[0] . $row[1]] = array(); } array_push($values[$row[0] . $row[1]], floatval($row[3])); } foreach ($values as $key => $val) { $rem = $ndays - count($val); for ($i = 0; $i < $rem; $i++) { array_push($val, 0); } $mean = mean($val); if ($mean < 1) { continue; } //Aviod super low page averages $sd = stdev($mean, $val); if ($sd == 0) { continue; } //Let's not deal with how this is even possible for right now $stdevs[$key] = array('mean' => $mean, 'stdev' => $sd, 'values' => $val); } $count = 100 * $vcount; $filter = ""; $dims = "ga:date,ga:hour,ga:hostname,ga:pagePath,ga:pageTitle"; $metric = "ga:pageviews"; $sort = "-ga:pageviews"; $data = runQuery($analytics, $account, $start, $end, $metric, $dims, $sort, $count, $filter); if (isset($data->ga_error)) { return DoNotCache(); } $data = $data->getRows(); $result = array(); foreach ($data as $key => $row) { $path = $row[2] . $row[3]; if (!isset($stdevs[$path])) { continue; } //if($sd['mean'] <= 0) continue; $sd = $stdevs[$path]; $z = zscore($sd['stdev'], $sd['mean'], $row[5]); $y = substr($row[0], 0, 4); $m = substr($row[0], 4, 2); $d = substr($row[0], 6, 2); $time = "{$y}-{$m}-{$d} " . $row[1] . ":00"; $ts = strtotime($time); $result[] = array('path' => $path, 'title' => $row[4], 'mean' => $sd['mean'], 'stdev' => $sd['stdev'], 'pageviews' => $row[5], 'z' => $z, 'timestamp' => $ts, "time" => $time, "values" => $sd['values']); } usort($result, "zsort"); $ret = array_splice($result, 0, $vcount); return $ret; }
function getEvents() { $start = tryGET('start'); $end = tryGET('end'); $lt = false; if (tryGet('longterm')) { $lt = true; } $loc = strtolower(tryGET('location')); //Get accounts $accounts = json_decode(file_get_contents('config/accounts.json'), true); $locAccounts = $accounts['location'][$loc]['accounts']; //print_r($accounts); $events = array(); foreach ($locAccounts as $key => $act) { $type = $act['type']; $import = false; if (isset($act["import"])) { $locArr = $act["locations"]; $import = true; } else { $id = $act["id"]; } //print $id; $te = array(); switch ($type) { case 'twitter': if (!$lt) { if ($import) { $te = getMultipleEvents($locArr, $type); } else { $te = topTweetEvents($id); } } break; case 'google analytics': $te = gaEvents($id); break; case 'facebook': if (!$lt) { if ($import) { $te = getMultipleEvents($locArr, $type); } else { $te = fbEvents($id); } } break; case 'instagram': if (!$lt) { if ($import) { $te = getMultipleEvents($locArr, $type); } else { $te = igEvents($id); } } break; } if (isset($te)) { $events = array_merge($events, $te); } } usort($events, "sortEvents"); $json = array("events" => $events); $json["start"] = $start * 1000; $json["end"] = $end * 1000; return $json; }