PiwikTracker implements the Piwik Tracking Web API. The PHP Tracking Client provides all features of the Javascript Tracker, such as Ecommerce Tracking, Custom Variable, Event tracking and more. Functions are named the same as the Javascript functions. See introduction docs at: {@link http://piwik.org/docs/tracking-api/} ### Example: using the PHP PiwikTracker class The following code snippet is an advanced example of how to track a Page View using the Tracking API PHP client. $t = new PiwikTracker( $idSite = 1, 'http://example.org/piwik/'); // Optional function calls $t->setUserAgent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB) Firefox/3.6.6"); $t->setBrowserLanguage('fr'); $t->setLocalTime( '12:34:06' ); $t->setResolution( 1024, 768 ); $t->setBrowserHasCookies(true); $t->setPlugins($flash = true, $java = true, $director = false); // set a Custom Variable called 'Gender' $t->setCustomVariable( 1, 'gender', 'male' ); // If you want to force the visitor IP, or force the server date time to a date in the past, // it is required to authenticate the Tracking request by calling setTokenAuth // You can pass the Super User token_auth or any user with 'admin' privilege on the website $idSite $t->setTokenAuth( $token_auth ); $t->setIp( "134.10.22.1" ); $t->setForceVisitDateTime( '2011-04-05 23:55:02' ); // if you wanted to force to record the page view or conversion to a specific User ID // $t->setUserId( "[email protected]" ); // Mandatory: set the URL being tracked $t->setUrl( $url = 'http://example.org/store/list-category-toys/' ); // Finally, track the page view with a Custom Page Title // In the standard JS API, the content of the tag would be set as the page title $t->doTrackPageView('This is the page title'); ### Example: tracking Ecommerce interactions Here is an example showing how to track Ecommerce interactions on your website, using the PHP Tracking API. Usually, Ecommerce tracking is done using standard Javascript code, but it is very common to record Ecommerce interactions after the fact (for example, when payment is done with Paypal and user doesn't come back on the website after purchase). For more information about Ecommerce tracking in Piwik, check out the documentation: Tracking Ecommerce in Piwik. $t = new PiwikTracker( $idSite = 1, 'http://example.org/piwik/'); // Force IP to the actual visitor IP $t->setTokenAuth( $token_auth ); $t->setIp( "134.10.22.1" ); // Example 1: on a Product page, track an "Ecommerce Product view" $t->setUrl( $url = 'http://www.mystore.com/Endurance-Shackletons-Legendary-Antarctic-Expedition' ); $t->setEcommerceView($sku = 'SKU0011', $name = 'Endurance - Shackleton', $category = 'Books'); $t->doTrackPageView( 'Endurance Shackletons Legendary Antarctic Expedition - Mystore.com'); // Example 2: Tracking Ecommerce Cart containing 2 products $t->addEcommerceItem($sku = 'SKU0011', $name = 'Endurance - Shackleton' , $category = 'Books', $price = 17, $quantity = 1); // Note that when setting a product category, you can specify an array of up to 5 categories to track for this product $t->addEcommerceItem($sku = 'SKU0321', $name = 'Amélie' , $categories = array('DVD Foreign','Best sellers','Our pick'), $price = 25, $quantity = 1); $t->doTrackEcommerceCartUpdate($grandTotal = 42); // Example 3: Tracking Ecommerce Order $t->addEcommerceItem($sku = 'SKU0011', $name = 'Endurance - Shackleton' , $category = 'Books', $price = 17, $quantity = 1); $t->addEcommerceItem($sku = 'SKU0321', $name = 'Amélie' , $categories = array('DVD Foreign','Best sellers','Our pick'), $price = 25, $quantity = 1); $t->doTrackEcommerceOrder($orderId = 'B000111387', $grandTotal = 55.5, $subTotal = 42, $tax = 8, $shipping = 5.5, $discount = 10); ### Note: authenticating with the token_auth To set the visitor IP, or the date and time of the visit, or to force to record the visit (or page, or goal conversion) to a specific Visitor ID, you must call setTokenAuth( $token_auth ). The token_auth must be either the Super User token_auth, or the token_auth of any user with 'admin' permission for the website you are recording data against. </div> <div class="tag-block tag-remarks"> </div> <div class="tag-block tag-exception"> </div> <div class="tag-block tag-see"> </div> <div class="tag-block tag-link"> </div> <div class="tag-block tag-deprecated"> </div> <div class="tag-block tag-since"> </div> <div class="tag-block tag-author"> </div> </div> </div> <div class="doc-tags"> </div> </div> </div> <div class="row" style="margin-bottom: 15px"> <div class="col-md-12"> <a class="btn btn-default" href="https://csharpdoc.hotexamples.com/fr/class/Piwik.Tracker/PiwikTracker"> PiwikTracker Class Documentation </a> </div> </div> </div> <div id="ezoic-pub-ad-placeholder-106"> </div> <div class="example-item" id="0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a-79,,112,"> <div class="bs-example"> <div> <div class="bs-example-title">Exemple #1</div> <div style="position: absolute;right: 15px;top: 10px;" id="rating_1" data-hash="0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a" data-area="79,,112,"> <div class="rating-amount">0</div> <a title="Noter cet exemple comme bon" href="javascript:app.addRating(1, '1')" class="rating-up btn btn-success" data-id="rating_1"><i class="icon-button icon-thumbs-up"></i></a> <a title="Noter cet exemple comme mauvais" href="javascript:app.addRating(-1,'1')" class="rating-down btn btn-danger" data-id="rating_1"><i class="icon-button icon-thumbs-down"></i></a> </div> </div> <div style="display:flex;align-items: center;flex-wrap: wrap"> <div style="display: inline-block;margin-right: 10px"> <a class="btn btn-default" title="Lien direct vers cet exemple" href="https://csharp.hotexamples.com/fr/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html#0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a-79,,112,"><i class="icon-button icon-link"></i></a> <a rel="nofollow" class="btn btn-default" href="javascript:app.open('https://csharp.hotexamples.com/fr/site/file?hash=0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a&fullName=Teknik%2FTracking.cs&project=Teknikode%2FTeknik')">Afficher le fichier</a> </div> <div class="example-project-info"> <nobr> <span>Fichier :</span> <a target="_blank" rel="nofollow" href="https://csharp.hotexamples.com/fr/site/redirect?url=https%3A%2F%2Fgithub.com%2FTeknikode%2FTeknik%2Fblob%2Fmaster%2FTeknik%2FTracking.cs">Tracking.cs</a> </nobr> <nobr> <span>Projet :</span> <a target="_blank" rel="nofollow" href="https://csharp.hotexamples.com/fr/site/redirect?url=https%3A%2F%2Fgithub.com%2FTeknikode%2FTeknik">Teknikode/Teknik</a> </nobr> </div> </div> </div> <figure class="highlight"> <div class="example" data-id="1"> <pre class="line-numbers language-csharp" data-end="112" data-start="80" data-highlight="80"> private static void TrackAction(HttpRequestBase request, Config config, string url, PiwikTracker.ActionType type) { // Handle Piwik Tracking if enabled if (config.PiwikConfig.Enabled) { try { // Follow Do Not Track string doNotTrack = request.Headers["DNT"]; if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1") { PiwikTracker.URL = config.PiwikConfig.Url; PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId); tracker.setUserAgent(request.UserAgent); string ipAddress = request.ClientIPFromRequest(true); tracker.setIp(ipAddress); tracker.setTokenAuth(config.PiwikConfig.TokenAuth); // Get Referral if (request.UrlReferrer != null) tracker.setUrlReferrer(request.UrlReferrer.ToString()); tracker.doTrackAction(url, type); } } catch (Exception ex) { } } }</pre> </div> </figure> </div> <div id="ezoic-pub-ad-placeholder-107"> <div style="text-align: center;margin-bottom: 10px"> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- after-example-1 --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3697522372581514" data-ad-slot="4981531786" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="example-item" id="0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a-13,,67,"> <div class="bs-example"> <div> <div class="bs-example-title">Exemple #2</div> <div style="position: absolute;right: 15px;top: 10px;" id="rating_2" data-hash="0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a" data-area="13,,67,"> <div class="rating-amount">0</div> <a title="Noter cet exemple comme bon" href="javascript:app.addRating(1, '2')" class="rating-up btn btn-success" data-id="rating_2"><i class="icon-button icon-thumbs-up"></i></a> <a title="Noter cet exemple comme mauvais" href="javascript:app.addRating(-1,'2')" class="rating-down btn btn-danger" data-id="rating_2"><i class="icon-button icon-thumbs-down"></i></a> </div> </div> <div style="display:flex;align-items: center;flex-wrap: wrap"> <div style="display: inline-block;margin-right: 10px"> <a class="btn btn-default" title="Lien direct vers cet exemple" href="https://csharp.hotexamples.com/fr/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html#0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a-13,,67,"><i class="icon-button icon-link"></i></a> <a rel="nofollow" class="btn btn-default" href="javascript:app.open('https://csharp.hotexamples.com/fr/site/file?hash=0x229da02857bb816968f3d3d0b12426c6a8dede1c28850dca757f559aef8cd80a&fullName=Teknik%2FTracking.cs&project=Teknikode%2FTeknik')">Afficher le fichier</a> </div> <div class="example-project-info"> <nobr> <span>Fichier :</span> <a target="_blank" rel="nofollow" href="https://csharp.hotexamples.com/fr/site/redirect?url=https%3A%2F%2Fgithub.com%2FTeknikode%2FTeknik%2Fblob%2Fmaster%2FTeknik%2FTracking.cs">Tracking.cs</a> </nobr> <nobr> <span>Projet :</span> <a target="_blank" rel="nofollow" href="https://csharp.hotexamples.com/fr/site/redirect?url=https%3A%2F%2Fgithub.com%2FTeknikode%2FTeknik">Teknikode/Teknik</a> </nobr> </div> </div> </div> <figure class="highlight"> <div class="example" data-id="2"> <pre class="line-numbers language-csharp" data-end="67" data-start="14" data-highlight="36"> public static void TrackPageView(HttpRequestBase request, Config config, string title) { // Handle Piwik Tracking if enabled if (config.PiwikConfig.Enabled) { try { // Follow Do Not Track string doNotTrack = request.Headers["DNT"]; if (string.IsNullOrEmpty(doNotTrack) || doNotTrack != "1") { string sub = request.RequestContext.RouteData.Values["sub"].ToString(); if (string.IsNullOrEmpty(sub)) { sub = request.Url.AbsoluteUri.GetSubdomain(); } if (config.DevEnvironment) { sub = "dev - " + sub; } PiwikTracker.URL = config.PiwikConfig.Url; PiwikTracker tracker = new PiwikTracker(config.PiwikConfig.SiteId); // Get Request Info string ipAddress = request.ClientIPFromRequest(true); tracker.setIp(ipAddress); tracker.setTokenAuth(config.PiwikConfig.TokenAuth); tracker.setUrl(request.Url.ToString()); tracker.setUserAgent(request.UserAgent); // Get browser info tracker.setResolution(request.Browser.ScreenPixelsWidth, request.Browser.ScreenPixelsHeight); tracker.setBrowserHasCookies(request.Browser.Cookies); if (!string.IsNullOrEmpty(request.Headers["Accept-Language"])) tracker.setBrowserLanguage(request.Headers["Accept-Language"]); BrowserPlugins plugins = new BrowserPlugins(); plugins.java = request.Browser.JavaApplets; tracker.setPlugins(plugins); // Get Referral if (request.UrlReferrer != null) tracker.setUrlReferrer(request.UrlReferrer.ToString()); // Send the tracking request tracker.doTrackPageView(string.Format("{0}/{1}", sub, title)); } } catch (Exception ex) { } } }</pre> </div> </figure> </div> <div id="all-examples"></div> </div> </div> </div> <div class="page-side-banner hidden-xs hidden-sm hidden-md" style="width:300px"> <div id="ezoic-pub-ad-placeholder-131"> <div class="fx" style="width:300px"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- right-sidebar-fx --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-3697522372581514" data-ad-slot="8968655429" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> </div> </div> <footer class="footer"> <div class="container"> <div class="row"> <div class="col-md-12" style="text-align: center"> <a href="https://csharp.hotexamples.com/fr/site/trends?type=csharp%7Cc"> Classes les mieux classées </a> | <a href="https://csharpdoc.hotexamples.com/fr/doc/map">Documentation</a> | <a href="/site/privacy">Politique de confidentialité</a> | <a href="https://cpp.hotexamples.com/direct-sales.html">Advertise with us</a> </div> </div> <div class="row"> <div class="col-md-12" style="text-align: center"> <a href="https://hotexamples.com/fr/">PHP</a> | <a href="https://csharp.hotexamples.com/fr/">C# (CSharp)</a> | <a href="https://java.hotexamples.com/fr/">Java</a> | <a href="https://golang.hotexamples.com/fr/">Golang</a> | <a href="https://cpp.hotexamples.com/fr/">C++ (Cpp)</a> | <a href="https://python.hotexamples.com/fr/">Python</a> | <a href="https://javascript.hotexamples.com/fr/">JavaScript</a> | <a href="https://typescript.hotexamples.com/fr/">TypeScript</a> </div> </div> <div class="row"> <div class="col-md-12" style="text-align: center"> <a href="https://csharp.hotexamples.com/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">EN</a> | <a href="https://csharp.hotexamples.com/ru/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">RU</a> | <a href="https://csharp.hotexamples.com/de/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">DE</a> | <a href="https://csharp.hotexamples.com/fr/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">FR</a> | <a href="https://csharp.hotexamples.com/es/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">ES</a> | <a href="https://csharp.hotexamples.com/pt/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">PT</a> | <a href="https://csharp.hotexamples.com/it/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">IT</a> | <a href="https://csharp.hotexamples.com/jp/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">JP</a> | <a href="https://csharp.hotexamples.com/zh/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">ZH</a> | <a href="https://csharp.hotexamples.com/ko/examples/Piwik.Tracker/PiwikTracker/-/php-piwiktracker-class-examples.html">KO</a> </div> </div> <div class="row"> <div class="col-md-10 col-md-offset-1"> </div> <div class="col-md-1"> <!--LiveInternet counter--> <script type="text/javascript"><!-- document.write("<a href='//www.liveinternet.ru/click' " + "target=_blank><img src='//counter.yadro.ru/hit?t44.6;r" + escape(document.referrer) + ((typeof (screen) == "undefined") ? "" : ";s" + screen.width + "*" + screen.height + "*" + (screen.colorDepth ? screen.colorDepth : screen.pixelDepth)) + ";u" + escape(document.URL) + ";" + Math.random() + "' alt='' title='LiveInternet' " + "border='0' width='31' height='31'><\/a>"); //--></script><!--/LiveInternet--> </div> </div> </div> </footer> <script src="//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="/assets/30dd86a6f06b64184847037c52c63e07aa3c9a26.js"></script> <script>jQuery(function ($) { jQuery('#search-form').yiiActiveForm([{"id":"searchform-lang","name":"lang","container":".field-searchform-lang","input":"#searchform-lang","enableAjaxValidation":true},{"id":"searchform-search","name":"search","container":".field-searchform-search","input":"#searchform-search","enableAjaxValidation":true,"validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Search ne peut être vide."});}}], []); });</script></body> </html>