Hi,
So when I started using Piwik we had about 3000 web sites to track. I did not want to have to create an entry for each site so I wrote this class. I include this on each site to generate and include the tracking code for each page.
I am not too great with PHP but this works for me. Let me know what you think and if it works for you.
Here is the class
<?php
// TRACKER
// Use this class to automatically create and track entries with Piwik
// Built for use with the javascript tracking api
// Expected inputs
// $url - Use $_SERVER['HTTP_HOST']
// $dbhost - Your Piwik database server
// $dbuser - Your Piwik database user
// $dbpass - Your Piwik database password
// $dbname - Your Piwik database name
// $apitoken - Your Piwik API token
// $piwik - The http path to your Piwik installation
class tracker {
private $url;
private $db;
private $id;
private $authtoken;
private $piwik;
function __construct($url,$dbhost,$dbuser,$dbpass,$dbname,$apitoken,$piwik) {
$this->set_url($url);
$this->set_db_conn($dbhost,$dbuser,$dbpass,$dbname);
$this->set_auth($apitoken);
$this->set_piwik($piwik);
$this->main();
}
function main() {
if ($this->find_site_info()) {
} else {
$this->create_site_entry();
$this->find_site_info();
}
}
function find_site_info() {
if ($result = $this->db->query("SELECT * FROM piwik_site WHERE name = '".$this->url."'")) {
while ($row = $result->fetch_array()) {
if (!empty($row['idsite'])) {
$this->id = $row['idsite'];
return true;
} else {
return false;
}
}
}
}
function create_site_entry() {
$ts_created = date('Y-d-m H:i:s');
// Use this string from the api to add a site
$token_auth = $this->authtoken; // API Authorization Token
$url = $this->piwik; // Where Piwik is hosted
$url .= "?module=API&method=SitesManager.addSite"; // Declare that we are using the API and that we are using the addSite function
$url .= "&siteName=".$this->url; // What we want the entry name to be
$url .= "&urls=".$this->url; // The url to track, look into passing an array
$url .= "&format=PHP&filter_limit=100";
$url .= "&token_auth=$token_auth";
$fetched = file_get_contents($url);
}
// SETTER FUNCTIONS //
function set_url($url) {
$nowww = str_replace('www\.','',$url);
$domain = parse_url($nowww);
if(!empty($domain["host"])) {
$this->url = $domain["host"];
} else {
$this->url = $domain["path"];
}
}
function set_db_conn($dbhost,$dbuser,$dbpass,$dbname) {
$mysqli = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$this->db = $mysqli;
}
function set_auth($authtoken) {
$this->authtoken = $authtoken;
}
function set_piwik($piwik) {
$this->piwik = $piwik;
}
// GETTER FUNCTIONS //
function get_id() {
return $this->id;
}
}
?>
Here is how I have been invoking it
<?php
$url = $_SERVER['host'];
$dbhost = "PIWIK DB SERVER";
$dbuser = "PIWIK DB USER";
$dbpass = "PIWIK DB PASSWORD";
$dbname = "PIWIK DB NAME";
$apitoken = "API TOKEN";
$piwik = "HTTP ADDRESS TO PIWIK";
$tracker = new tracker($url,$dbhost,$dbuser,$dbpass,$dbname,$apitoken,$piwik);
?>
<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "PIWIK URL" : "PIWIK URL");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", <?php echo $tracker->get_id(); ?>);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="PIWIK URL/piwik.php?idsite=<?php echo $tracker->get_id(); ?>" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->