The following features require access:
- force the visitor IP
- force the date & time of the tracking requests rather than track for the current datetime
public setTokenAuth ( string $token_auth ) | ||
$token_auth | string | token_auth 32 chars token_auth string |
function saveInPiwik($logHash) { echo $logHash["ip"] . " " . $logHash["status"] . " " . $logHash["utcdatetime"] . " " . $logHash["path"] . " (" . $logHash["agent"] . ")... "; global $idSite, $webUrl, $piwikUrl, $tokenAuth; $t = new PiwikTracker($idSite, $piwikUrl); $t->setUserAgent($logHash["agent"]); $t->setTokenAuth($tokenAuth); $t->setIp($logHash["ip"]); $t->setForceVisitDateTime($logHash["utcdatetime"]); $t->setUrlReferrer($logHash["referer"]); $t->setUrl($webUrl . $logHash["path"]); $HTTPResult = false; $HTTPFailCount = 0; do { $HTTPResult = $t->doTrackPageView(basename($logHash["path"])); if (!$HTTPResult) { $HTTPFailCount++; echo "FAIL\n"; echo "Unable to save (via HTTP) last log entry for the {$HTTPFailCount} time, retrying in a few seconds...\n"; sleep($HTTPFailCount); } else { echo "SUCCESS\n"; } } while (!$HTTPResult); }
/** * Inicia a análise de tráfego. */ private function iniciarAnaliseTrafego() { if ($this->_configuracao->get($this->_servidor . '.piwik_id')) { require_once implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', '..', '..', 'library', 'PiwikTracker.php']); $piwikTracker = new \PiwikTracker($this->_configuracao->get($this->_servidor . '.piwik_id'), $this->_configuracao->get($this->_servidor . '.piwik_url')); if ($this->_configuracao->get($this->_servidor . '.piwik_token_auth')) { $piwikTracker->setTokenAuth($this->_configuracao->get($this->_servidor . '.piwik_token_auth')); } if (isset($_SERVER['HTTP_REFERER'])) { $piwikTracker->setReferrer($_SERVER['HTTP_REFERER']); } // detecta o endereço da internet do cliente if (isset($_SERVER['HTTP_CLIENT_IP'])) { $piwikTracker->setIp($_SERVER['HTTP_CLIENT_IP']); } else { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $piwikTracker->setIp($_SERVER['HTTP_X_FORWARDED_FOR']); } else { if (isset($_SERVER['HTTP_X_FORWARDED'])) { $piwikTracker->setIp($_SERVER['HTTP_X_FORWARDED']); } else { if (isset($_SERVER['HTTP_FORWARDED_FOR'])) { $piwikTracker->setIp($_SERVER['HTTP_FORWARDED_FOR']); } else { if (isset($_SERVER['HTTP_FORWARDED'])) { $piwikTracker->setIp($_SERVER['HTTP_FORWARDED']); } else { if (isset($_SERVER['REMOTE_ADDR'])) { $piwikTracker->setIp($_SERVER['REMOTE_ADDR']); } else { $piwikTracker->setIp('DESCONHECIDO'); } } } } } } $piwikTracker->setUserAgent($_SERVER['HTTP_USER_AGENT']); if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $idioma = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); $piwikTracker->setBrowserLanguage($idioma[0]); unset($idioma); } if ($this->postExists('localTime')) { $piwikTracker->setLocalTime($this->post('localTime')); } if ($this->postExists('screenWidth') && $this->postExists('screenHeight')) { $piwikTracker->setResolution($this->post('screenWidth'), $this->post('screenHeight')); } if ($this->postExists('position')) { $posicao = $this->post('position'); $piwikTracker->setLongitude($posicao['longitude']); $piwikTracker->setLatitude($posicao['latitude']); unset($posicao); } } else { $piwikTracker = null; } return $piwikTracker; }
/** * 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; } }
/** * Returns a PiwikTracker object that you can then use to track pages or goals. * * @param int $idSite * @param string $dateTime * @param boolean $defaultInit If set to true, the tracker object will have default IP, user agent, time, resolution, etc. * @param bool $useLocal * * @return PiwikTracker */ public static function getTracker($idSite, $dateTime, $defaultInit = true, $useLocal = false) { if ($useLocal) { require_once PIWIK_INCLUDE_PATH . '/tests/LocalTracker.php'; $t = new Piwik_LocalTracker($idSite, self::getTrackerUrl()); } else { $t = new PiwikTracker($idSite, self::getTrackerUrl()); } $t->setForceVisitDateTime($dateTime); if ($defaultInit) { $t->setTokenAuth(self::getTokenAuth()); $t->setIp('156.5.3.2'); // Optional tracking $t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)"); $t->setBrowserLanguage('fr'); $t->setLocalTime('12:34:06'); $t->setResolution(1024, 768); $t->setBrowserHasCookies(true); $t->setPlugins($flash = true, $java = true, $director = false); } return $t; }
function track($campaign, $keyword) { # Piwik URL PiwikTracker::$URL = $GLOBALS['config']['piwik_url']; # PiwikTracker $piwik = new PiwikTracker($GLOBALS['config']['idsite']); # required for setIP() $piwik->setTokenAuth($GLOBALS['config']['piwik_token']); # IP of the remote client $piwik->setIP($_SERVER['REMOTE_ADDR']); # User Agent String of the client $piwik->setUserAgent($_SERVER['HTTP_USER_AGENT']); # get the schema $schema = 'https://'; if (empty($_SERVER['HTTPS']) or $_SERVER['HTTPS'] === 'off') { $schema = 'http://'; } # set the tracked URL $piwik->setUrl($schema . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); # referer $piwik->setUrlReferrer($_SERVER['HTTP_REFERER']); # set attribution info (keyword, campaign, et al) # http://developer.piwik.org/api-reference/PHP-Piwik-Tracker#setattributioninfo # 0 - campaign name # 1 - keyword # 2 - timestamp # 3 - referrer $att = json_encode(array($campaign, $keyword, time(), $_SERVER['HTTP_REFERER'])); $piwik->setAttributionInfo($att); if (defined("DEBUG") and DEBUG) { header("X-Debug-Referred: from " . $_SERVER['HTTP_REFERER']); header("X-Debug-AttInfo: {$att}"); } # do track the page view if (empty($keyword)) { $keyword = 'none'; } $piwik->doTrackPageView("Reprint Tracker for: {$campaign} (keyword: {$keyword})"); if (defined("DEBUG") and DEBUG) { header('X-Last-Piwik-Call: ' . $piwik::$DEBUG_LAST_REQUESTED_URL); } }