Need to auto add websites via API or upload

Hi there, I am a new user (and long time PHP developer) and I’m just loving what I see so far. I’m thinking of using Piwik to manage potentially 1000’s of websites - actually they would be individual pages that are set up as a site that we need to track and report on individually!

I was wondering if there is a way to upload a .csv file of the urls, etc. ?

Or is there an API call that will create a website? If so, I could then write a simple program to insert each of our sites automatically. This would be needed after the initial load for new sites/pages as necessary.

Also, it would be great if I could use our own website id as the site id, to make coordinating our tasks much easier.

Cheers,
Royce

Or is there an API call that will create a website?

Sure, see: http://developer.piwik.org/api-reference/reporting-api#SitesManager

Unfortunately there is no example.
I have tried to use a php curl call to add a site to piwik, but I always get the message that the action does not exist in the named module.

I have tried:

Action ‘addSite’, module 'SitesManager’
Action SitesManager.‘addSite’, module ‘API’ (thats how its called in the piwik web interface)

which values should these parameters have?

Ok , try this: ?module=API&method=SitesManager.addSite

Did you succeed?

This code works using file_get_contents rather than curl, but shows you the basics. You don’t actually need the URL as Piwik tracks with teh site-id (returned on the addSite call)


 <?php
// this token is used to authenticate your API request. 
// You can get the token on the API page inside your Piwik interface
$token_auth = 'gdgdgdghddggdhdghddfghdfgh';

$file = fopen("input.csv","r");
//  csv name, url

while(! feof($file))
  {
  	$row=fgetcsv($file);
  	print_r($row);
  	echo "<br>";

	$url = "https://analytics-02.mydomain.com/";
	$url .= "?module=API&method=SitesManager.addSite";
	$url .= "&siteName=".urlencode($row[0]);
	$url .= "&urls=".$row[1];
	$url .= "&token_auth=$token_auth";


	$fetched = file_get_contents($url);
	print_r ($fetched);
	echo "<br>";
  }
fclose($file);
?>


“?module=API&method=SitesManager.addSite”

did not work. I get the same message.

Now I will try the "file_get_contents"
It does work like a GET-Request, doesn´t it?

It is a GET request (hence https in my example), code I used is based on part 1 of the following page, just simpler than all those CURL opt stuff

http://developer.piwik.org/guides/querying-the-reporting-api

Perhaps you should post up your CURL call code (changing the token & url for security) see if any one can spot what is wrong / missing

Nothing works, do I have to change some preferences in piwik?

Here the code of my curl call:


$p = array();
$p['module'] = "API";
$p['action'] = "SitesManager.addSite";
$p['format'] = "json";

$p['siteName'] = "testSeite";
$p['urls'] = "www.test.com";
$p['token_auth'] = "somehash";

$header = array(
    'Connection: keep-alive',
    'Pragma: no-cache',
    'Cache-Control: no-cache'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_URL, $_SERVER["HTTP_HOST"] . "/apps/piwik/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($p));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$string = curl_exec($ch);
echo $string;
curl_close($ch);

and this the same call with file_get_contents:


$p = array();
$p['module'] = "API";
$p['action'] = "SitesManager.addSite";
$p['format'] = "json";

$p['siteName'] = "testSeite";
$p['urls'] = "www.test.com";
$token_auth = "somehash";

$url = "http://" . $_SERVER["HTTP_HOST"] . "/apps/piwik/index.php?"  . http_build_query($p);
$url .= "&token_auth=$token_auth";
var_dump($url);

$fetched = file_get_contents($url);
print_r ($fetched);
echo "<br>";

exit;

Without testing your code, the main difference I spot is $p[‘format’] = “json”;

Perhaps json response is not supported in that module, after all it just returns an integer

p.s. just added json to my code an it worked fine

OK I have got it


$p['action'] = "SitesManager.addSite";

needs to be


$p['method'] = "SitesManager.addSite";

You owe me a beer

Oh yes, I do. Of course it works now.

F*ck. I don’t know why I replaced “method” by “action”.

I call it code blindness, you get so familiar with the code you don’t notice the obvious. All ways good to get an outsider to look, even if they are not technical, I have found just talking someone through a problem spurs a different thought process.

Thats it. I had never checked the names of the request parameters.
Thank you for your time. And the result :wink:

Hey there, thanks for the help with understanding how I can add new sites. Our main application essentially manages websites and we want to use Piwik to do the analytics for each of the managed sites.

Because of this, it would be really helpful if I could specify the site_id (instead of relying on the database auto-increment value) when calling the Piwik API to create a new site.

Does anyone know how I could do that?

If necessary, I could write a plugin (or override the existing code if that is possible) to be able to pass the id to the Piwik add website function.

Thanks in advance for any ideas or suggestions.

regards,
Royce

It is quite easy to query the Piwik ID before hand, there are lots of SitesManager.getSites… methods . That is for instance how the Multi-Site Wordpress plugin handles it.

So once you have added a site you can always get it back, e.g. using SitesManager.getSitesIdFromSiteUrl

If you do it your way I think you would be fighting the system!