/** * Sends the keyword and destination URL to Piwik * * @param bool $return The value to return. Defaults to false with doesn't enable the filter * @param string $keyword The requested keyword * @return bool */ function itfs_piwik_log_request($return, $keyword) { // Get current configuration from database $piwik_config = yourls_get_option('piwik_config'); // Let's check if the user wants to log bots if ($piwik_config[remove_bots]) { if (itfs_piwik_is_bot()) { return $return; } } try { // Need to use a file_exists check as require only produces a fatal compilation error if (!file_exists(dirname(__FILE__) . '/libs/Piwik/PiwikTracker.php')) { throw new Exception("Error can't load PiwikTracker.php"); } else { // Piwik Tracking API init require_once dirname(__FILE__) . '/libs/Piwik/PiwikTracker.php'; PiwikTracker::$URL = $piwik_config[piwik_url]; } } catch (Exception $e) { error_log("ITFS_PIWIK: " . $e->getMessage(), 0); return $return; } // Use this to get the destination $destination = yourls_get_keyword_longurl($keyword); // Only log a request if we have a destination and the proper Piwik settings if ($destination == false) { error_log("ITFS_PIWIK: Missing parameters prevented me from logging the request with Piwik", 0); error_log("ITFS_PIWIK: Parameters we have: " . $keyword . ', ' . $destination, 0); return $return; } //Useful for hosts using one Piwik installation with multiple YOURLS installation $domain_landed = $_SERVER['HTTP_HOST']; $page_url = "http://" . $domain_landed . "/" . $keyword; try { $pt = new PiwikTracker($piwik_config[site_id]); // This will be the entry page in Piwik $pt->setUrl($page_url); // This will fail silently if the token is not valid or if the user doesn't have admin rights if (!empty($piwik_config[token])) { $pt->setTokenAuth($piwik_config[token]); } // This shows up in the visitor logs and identify the source of the data $pt->setCustomVariable(1, 'App', 'Piwik plugin for YOURLS', 'visit'); // Some useful variables $pt->setCustomVariable(2, 'Domain landed', $domain_landed, 'page'); $pt->setCustomVariable(3, 'Keyword', $keyword, 'page'); // User defined custom variable if (!empty($piwik_config[customvar_name]) && !empty($piwik_config[customvar_value])) { $pt->setCustomVariable(4, $piwik_config[customvar_name], $piwik_config[customvar_value], $piwik_config[customvar_scope]); } // Track the visit in Piwik $title = yourls_get_keyword_title($keyword); @$pt->doTrackPageView($title); // The destination URL will show up as an outlink @$pt->doTrackAction($destination, 'link'); } catch (Exception $e) { error_log("ITFS_PIWIK: Error when trying to log the request with Piwik. " . $e->getMessage(), 0); return $return; } if ($piwik_config[disable_stats]) { //error_log("ITFS_PIWIK: NOT logging locally", 0); return; } else { //error_log("ITFS_PIWIK: Logging locally", 0); return $return; } }