Independent Analytics includes a simple developer API for anyone who wants to access their data outside of the analytics dashboard.
As of now, the API includes two simple functions, as documented below.
iawp_analytics(DateTime $from, DateTime $to)
Gets analytics data from the given date range. Includes views, visitors, and sessions.
Parameters
- $from
- Type: DateTime
- Required
- Description: This is the starting date for your selected date range.
- $to
- Type: DateTime
- Required
- Description: This is the ending date for your selected date range.
Return
Returns an IAWP\Public_API\Analytics object with three properties: views, visitors, and sessions.
IAWP\Public_API\Analytics { views: 800, visitors: 325, sessions: 355 }
Example
Here’s how you would get data from the past three days and output the number of views:
$analytics = iawp_analytics(new DateTime('-3 days'), new DateTime());
echo $analytics->views;
iawp_singular_analytics(int $singular_id, DateTime $from, DateTime $to)
Gets analytics data from the given date range for a specific page. Includes views, visitors, and sessions.
Parameters
- $singular_id
- Type: int
- Required
- Description: This is the ID of the page you want to get stats for.
- $from
- Type: DateTime
- Required
- Description: This is the starting date for your selected date range.
- $to
- Type: DateTime
- Required
- Description: This is the ending date for your selected date range.
Return
Returns an IAWP\Public_API\Singular_Analytics object with the three properties: views, visitors, and sessions.
IAWP\Public_API\Singular_Analytics { views: 400, visitors: 125, sessions: 255 }
Example
Here’s how you would get data from the last seven days for a page with an ID of “60” and output the sessions:
$singular_analytics = iawp_singular_analytics(60, new DateTime('-7 days'), new DateTime());
echo $singular_analytics->sessions;
iawp_top_posts(array $arguments)
This function gets the top posts by view count. It returns each post’s ID, title, views, visitors, and sessions.
Arguments:
- post_type
- Type: string
- Optional
- Default: post
- Description: The post type you want to fetch, such as
post
orpage
.
- limit
- Type: int
- Optional
- Default: 10
- Description: The maximum number of posts to fetch.
- from
- Type: DateTime
- Optional
- Default: 30 days ago
- Description: The starting date to check from.
- to
- Type: DateTime
- Optional
- Default: today
- Description: The ending date to check up to
- sort_by
- Type: string
- Optional
- Default: views
- Description: acceptable values are
views
,visitors
, orsessions
Return
Returns an array of objects, each representing one post. Each object includes the ID, title, views, visitors, and sessions.
array(2) {
[0]=>
object(stdClass)#774 (5) {
["id"]=>
int(1270)
["title"]=>
string(14) "This is a Post"
["views"]=>
int(215)
["visitors"]=>
int(172)
["sessions"]=>
int(186)
}
[1]=>
object(stdClass)#776 (5) {
["id"]=>
int(1272)
["title"]=>
string(17) "Another test post"
["views"]=>
int(199)
["visitors"]=>
int(140)
["sessions"]=>
int(153)
}
}
Example
Here’s how you could get the top 10 posts sorted by visitors from the last 30 days, and create a simple clickable list from them:
$posts = iawp_top_posts([
'post_type' => 'posts',
'limit' => 10,
'from' => new DateTime('-30 days'),
'to' => new DateTime('now'),
'sort_by' => 'visitors'
]);
<div>
<ul>
<?php foreach($posts as $post) {
echo '<li>';
echo '<a href="'. get_permalink($post->id) .'">'. $post->title .'</a>';
echo ' ('. $post->views .' Views)';
echo '</li>';
} ?>
</ul>
</div>
Learn how to use Independent Analytics in bite-sized email lessons delivered every 1-3 days.
Signup Here