{"id":5063,"date":"2024-01-17T11:33:22","date_gmt":"2024-01-17T16:33:22","guid":{"rendered":"https:\/\/independentwp.com\/?post_type=kb_article&#038;p=5063"},"modified":"2025-10-15T12:51:17","modified_gmt":"2025-10-15T16:51:17","slug":"developer-api","status":"publish","type":"kb_article","link":"https:\/\/independentwp.com\/knowledgebase\/developer\/developer-api\/","title":{"rendered":"How to Use the Developer API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Independent Analytics includes a simple developer API for anyone who wants to access their data outside of the analytics dashboard.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As of now, the API includes two simple functions, as documented below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">iawp_analytics(DateTime $from, DateTime $to)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Gets analytics data from the given date range. Includes views, visitors, and sessions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$from<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Required<\/strong><\/li>\n\n\n\n<li><strong>Description: <\/strong>This is the starting date for your selected date range.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>$to<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Required<\/strong><\/li>\n\n\n\n<li><strong>Description: <\/strong>This is the ending date for your selected date range.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Return<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Returns an <strong>IAWP\\Public_API\\Analytics<\/strong> object with three properties: views, visitors, and sessions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">IAWP\\Public_API\\Analytics {\n  views: 800,\n  visitors: 325,\n  sessions: 355\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s how you would get data from the past three days and output the number of views:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$analytics = iawp_analytics(new DateTime('-3 days'), new DateTime());\necho $analytics-&gt;views;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">iawp_singular_analytics(int $singular_id, DateTime $from, DateTime $to)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Gets analytics data from the given date range for a specific page. Includes views, visitors, and sessions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>$singular_id<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> int<\/li>\n\n\n\n<li><strong>Required<\/strong><\/li>\n\n\n\n<li><strong>Description: <\/strong>This is the ID of the page you want to get stats for.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>$from<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Required<\/strong><\/li>\n\n\n\n<li><strong>Description: <\/strong>This is the starting date for your selected date range.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>$to<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Required<\/strong><\/li>\n\n\n\n<li><strong>Description: <\/strong>This is the ending date for your selected date range.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Return<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Returns an <strong>IAWP\\Public_API\\Singular_Analytics<\/strong> object with the three properties: views, visitors, and sessions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">IAWP\\Public_API\\Singular_Analytics {\n  views: 400,\n  visitors: 125,\n  sessions: 255\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s how you would get data from the last seven days for a page with an ID of &#8220;60&#8221; and output the sessions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$singular_analytics = iawp_singular_analytics(60, new DateTime('-7 days'), new DateTime());\necho $singular_analytics-&gt;sessions;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">iawp_top_posts(array $arguments)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This function gets the top posts by view count. It returns each post&#8217;s ID, title, views, visitors, and sessions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Arguments<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>post_type<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> string<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> post<\/li>\n\n\n\n<li><strong>Description: <\/strong>The post type you want to fetch, such as <code>post<\/code> or <code>page<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>category<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> ?int<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> null<\/li>\n\n\n\n<li><strong>Description:<\/strong> Choose one category to fetch the posts from.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>limit<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> int<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> 10<\/li>\n\n\n\n<li><strong>Description: <\/strong>The maximum number of posts to fetch.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>from<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> 30 days ago<\/li>\n\n\n\n<li><strong>Description: <\/strong>The starting date to check from.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>to<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> DateTime<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> today<\/li>\n\n\n\n<li><strong>Description:<\/strong> The ending date to check up to<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>sort_by<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Type:<\/strong> string<\/li>\n\n\n\n<li><strong>Optional<\/strong><\/li>\n\n\n\n<li><strong>Default:<\/strong> views<\/li>\n\n\n\n<li><strong>Description<\/strong>: acceptable values are <code>views<\/code>, <code>visitors<\/code>, or <code>sessions<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Return<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Returns an array of objects, each representing one post. Each object includes the ID, title, views, visitors, and sessions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">array(2) {<br>  [0]=&gt;<br>  object(stdClass)#774 (5) {<br>    [\"id\"]=&gt;<br>    int(1270)<br>    [\"title\"]=&gt;<br>    string(14) \"This is a Post\"<br>    [\"views\"]=&gt;<br>    int(215)<br>    [\"visitors\"]=&gt;<br>    int(172)<br>    [\"sessions\"]=&gt;<br>    int(186)<br>  }<br>  [1]=&gt;<br>  object(stdClass)#776 (5) {<br>    [\"id\"]=&gt;<br>    int(1272)<br>    [\"title\"]=&gt;<br>    string(17) \"Another test post\"<br>    [\"views\"]=&gt;<br>    int(199)<br>    [\"visitors\"]=&gt;<br>    int(140)<br>    [\"sessions\"]=&gt;<br>    int(153)<br>  }<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s how you could get the top 10 posts in a category with the ID 5, sorted by visitors from the last 30 days. Then, create a simple clickable list from them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$posts = iawp_top_posts(&#91;\n\t'post_type' => 'posts',\n        'category' => 5,\n\t'limit' => 10, \n\t'from' => new DateTime('-30 days'), \n\t'to' => new DateTime('now'), \n\t'sort_by' => 'visitors'\n]);\n\n&lt;div>\n  &lt;ul>\n    &lt;?php foreach($posts as $post) {\n      echo '&lt;li>';\n      echo '&lt;a href=\"'. get_permalink($post->id) .'\">'. $post->title .'&lt;\/a>';\n      echo ' ('. $post->views .' Views)';\n      echo '&lt;\/li>';\n    } ?>\n  &lt;\/ul>\n&lt;\/div> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Independent Analytics has a simple new developer API that lets you get data for your site and individual pages. Here&#8217;s how it works.<\/p>\n","protected":false},"parent":0,"template":"","kb_article_category":[27],"class_list":["post-5063","kb_article","type-kb_article","status-publish","hentry","kb_article_category-developer","entry"],"acf":[],"_links":{"self":[{"href":"https:\/\/independentwp.com\/wp-json\/wp\/v2\/kb_article\/5063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/independentwp.com\/wp-json\/wp\/v2\/kb_article"}],"about":[{"href":"https:\/\/independentwp.com\/wp-json\/wp\/v2\/types\/kb_article"}],"wp:attachment":[{"href":"https:\/\/independentwp.com\/wp-json\/wp\/v2\/media?parent=5063"}],"wp:term":[{"taxonomy":"kb_article_category","embeddable":true,"href":"https:\/\/independentwp.com\/wp-json\/wp\/v2\/kb_article_category?post=5063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}