Forums » Developers

Sample PHP Code

I have been using the csv files, but its clear I need to move beyond that.

So my plan is to use PHP and the API. I have been reading through here and the Docs, but I don't see what I am looking for. I am looking for some sample php code that I could copy to my system and make some changes like sitekey and others to make it retrieve my site info.

Just a basic program or a few lines of code with the proper syntax so I could get started. I am most concerned about proper syntax.

Sometimes you can spend allot of time just getting a basic result. Samples are really helpful.

Thank You

Posted Sun Jun 5 2011 7:35am by ucare

As long as you know how to access array keys and/or loop through an array, that's really all you need.

If you copy/paste the following code into a PHP file, it will output the structure of the PHP array returned by this query (from our demo stats)

$x = unserialize( file_get_contents(",actions,actions-average,time-average,bounce-rate&output=php"));
print_r( $x );

Posted Sun Jun 5 2011 9:47am by Your Friendly Clicky Admin

Thank You. Thats exactly what I was looking for !

Posted Sun Jun 5 2011 3:59pm by ucare

Okay, I have been testing the API. It loads the data very fast. I have not yet figuered out how to traverse through the array, but I will keep trying.

Now when I use the "pages-entrance" I don't get what I expected. I expected like the CSV but more.
For instance in the "pages-entrance" I also want to see search terms.

Thank You.

Posted Tue Jun 7 2011 3:37am by ucare

So with this code:
$x = unserialize( file_get_contents("****8&sitekey=0fa******46f62&type=visitors,searches,pages-entrance,bounce-rate&output=php"));

I just want to confirm. Its returns 10 records. the array for searches is perfectly synchronized with the array for entrance pages right?
So the first value in the search array is the search term for the first value in the array for entrance pages.

Posted Tue Jun 7 2011 11:11pm by ucare

Because what I really want is the entrance pages and the search terms. Now some entrance pages have no search therms because they are a link or an ad.

Posted Wed Jun 8 2011 12:12am by ucare

No, they are unrelated. We don't have a report that correlates landing pages with search terms, although that is something we want to add!

Posted Wed Jun 8 2011 12:16am by Your Friendly Clicky Admin

Okay, everything seems to be working fine, except I am not getting large amounts data.
I only get 186 visitors info.

$x = unserialize( file_get_contents("*****68&sitekey=0fa4******dd346f62&type=visitors,visitors-list&limit=all&&date=2011-05-01,2011-05-15,bounce-rate&output=php"));

Posted Wed Jun 15 2011 12:46am by ucare

Error in code, here is the correct code:
$x = unserialize( file_get_contents("*****8&sitekey=0fa*********62&type=visitors,visitors-list&limit=all&date=2011-05-01,2011-05-15,bounce-rate&output=php"));

The other troubling thing is I get different amounts each time. Now getting 190, then 196.

Posted Wed Jun 15 2011 12:58am by ucare

This is wrong:


Our code will see that as an invalid date range and replace it with "today". Hence why the number is growing, because it's for today's stats.

Posted Wed Jun 15 2011 9:08am by Your Friendly Clicky Admin

Yes, I see the dates are current. So what would be the correct syntax for visitors-list, limit all, date range?

Posted Wed Jun 15 2011 11:59am by ucare

Everything is right you just need to remove ",bounce-rate" from the date variable.

Posted Wed Jun 15 2011 1:16pm by Your Friendly Clicky Admin

Be careful using file_get_contents over and over as it doesn't close connections right away.

Posted Sun Nov 23 2014 3:06pm by toddw

Reply to this discussion

You must be logged in to your account to post!