function sendgrid_sga_data_api()
{
    header("Content-Type: application/json");
    if ($_POST['api_key'] != get_option("sendgrid_sga_apikey")) {
        $output = array("error" => true, "message" => "Unauthenticated.");
        echo json_encode($output);
        die;
    }
    $period_start = create_start_time($_POST['period_start'], true);
    $period_end = create_end_time($_POST['period_end'], true);
    create_table($period_start, $period_end);
    $output = array("error" => true, "message" => "Unknown error.");
    $body = array();
    if ($_POST['data'] == "users") {
        $top_users = get_top_users();
        foreach ($top_users as $rank => $user_info) {
            $formatted_user_info = format_user_info($user_info);
            $body[] = array("raw" => array("post_author" => get_post_author($user_info->post_author), "mantime" => (int) $user_info->mantime, "visits" => (int) $user_info->visits, "pageviews" => (int) $user_info->pageviews, "avg_time_on_page" => (double) $user_info->avg_time_on_page, "entrance_rate" => (double) $user_info->entrance_rate, "exit_rate" => (double) $user_info->exit_rate), "formatted" => $formatted_user_info);
        }
        $output = array("error" => false, "body" => $body, "period_start" => $period_start, "period_end" => $period_end);
    } elseif ($_POST['data'] == "posts") {
        $top_posts = get_top_posts();
        foreach ($top_posts as $rank => $post_info) {
            $formatted_post_info = format_post_info($post_info);
            $body[] = array("raw" => array("guid" => $post_info->guid, "post_title" => $post_info->post_title, "post_author" => get_post_author($post_info->post_author), "mantime" => (int) $post_info->mantime, "visits" => (int) $post_info->visits, "pageviews" => (int) $post_info->pageviews, "avg_time_on_page" => (double) $post_info->avg_time_on_page, "entrance_rate" => (double) $post_info->entrance_rate, "exit_rate" => (double) $post_info->exit_rate), "formatted" => $formatted_post_info);
        }
        $output = array("error" => false, "body" => $body, "period_start" => $period_start, "period_end" => $period_end);
    } else {
        $output = array("error" => true, "message" => "Data attribute not understood, please provide either users or posts");
    }
    echo json_encode($output);
    die;
}
					Average Time on Page
				</th>
				<th title="Entrance rate is the percent of people who came to SendGrid.com via this post." class="tooltip">
					Entrance Rate
				</th>
				<th title="Exit rate is the percent of people who left SendGrid.com after reading this post." class="tooltip">
					Exit Rate
				</th>
			</tr>
		</thead>
		<tbody>
			<?php 
foreach ($top_posts as $rank => $post_info) {
    ?>
			<?php 
    $formatted_post_info = format_post_info($post_info);
    ?>
			<tr>
				<td>
					<?php 
    echo $rank + 1;
    ?>
				</td>
				<td>
					<a href="<?php 
    echo $formatted_post_info["guid"];
    ?>
"><?php 
    echo $formatted_post_info["post_title"];
    ?>
</a> by <?php