PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/PHPUnit/Fixtures/TwoVisitsWithCustomVariables.php

https://github.com/CodeYellowBV/piwik
PHP | 149 lines | 114 code | 18 blank | 17 comment | 6 complexity | af103cd4f215e3965d5c2e500236e501 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. use Piwik\Date;
  9. use Piwik\Plugins\Goals\API;
  10. /**
  11. * Adds one site with two goals and tracks two visits with custom variables.
  12. */
  13. class Test_Piwik_Fixture_TwoVisitsWithCustomVariables extends Fixture
  14. {
  15. public $dateTime = '2010-01-03 11:22:33';
  16. public $idSite = 1;
  17. public $idGoal1 = 1;
  18. public $idGoal2 = 2;
  19. public $visitorId = '61e8cc2d51fea26d';
  20. public $useEscapedQuotes = true;
  21. public $doExtraQuoteTests = true;
  22. public $resolutionWidthToUse = 1111;
  23. public $resolutionHeightToUse = 222;
  24. public function setUp()
  25. {
  26. $this->setUpWebsitesAndGoals();
  27. $this->trackVisits();
  28. }
  29. public function tearDown()
  30. {
  31. // empty
  32. }
  33. private function setUpWebsitesAndGoals()
  34. {
  35. // tests run in UTC, the Tracker in UTC
  36. if (!self::siteCreated($idSite = 1)) {
  37. self::createWebsite($this->dateTime);
  38. }
  39. if (!self::goalExists($idSite = 1, $idGoal = 1)) {
  40. API::getInstance()->addGoal($this->idSite, 'triggered js', 'manually', '', '');
  41. }
  42. if (!self::goalExists($idSite = 1, $idGoal = 2)) {
  43. API::getInstance()->addGoal($this->idSite, 'second goal', 'manually', '', '');
  44. }
  45. }
  46. private function trackVisits()
  47. {
  48. $dateTime = $this->dateTime;
  49. $idSite = $this->idSite;
  50. $idGoal = $this->idGoal1;
  51. $idGoal2 = $this->idGoal2;
  52. $visitorA = self::getTracker($this->idSite, $this->dateTime, $defaultInit = true);
  53. // Used to test actual referrer + keyword position in Live!
  54. $visitorA->setUrlReferrer(urldecode('http://www.google.com/url?sa=t&source=web&cd=1&ved=0CB4QFjAA&url=http%3A%2F%2Fpiwik.org%2F&rct=j&q=this%20keyword%20should%20be%20ranked&ei=V8WfTePkKKLfiALrpZWGAw&usg=AFQjCNF_MGJRqKPvaKuUokHtZ3VvNG9ALw&sig2=BvKAdCtNixsmfNWXjsNyMw'));
  55. // no campaign, but a search engine to attribute the goal conversion to
  56. $attribution = array(
  57. '',
  58. '',
  59. 1302306504,
  60. 'http://www.google.com/search?q=piwik&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a'
  61. );
  62. $visitorA->setAttributionInfo(json_encode($attribution));
  63. $visitorA->setResolution($this->resolutionWidthToUse, $this->resolutionHeightToUse);
  64. // At first, visitor custom var is set to LoggedOut
  65. $visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->getDatetime());
  66. $visitorA->setUrl('http://example.org/homepage');
  67. $visitorA->setCustomVariable($id = 1, $name = 'VisitorType', $value = 'LoggedOut');
  68. self::checkResponse($visitorA->doTrackPageView('Homepage'));
  69. self::checkResponse($visitorA->doTrackGoal($idGoal2));
  70. // After login, set to LoggedIn, should overwrite previous value
  71. $visitorA->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.2)->getDatetime());
  72. $visitorA->setUrl('http://example.org/user/profile');
  73. $visitorA->setCustomVariable($id = 1, $name = 'VisitorType', $value = 'LoggedIn');
  74. $visitorA->setCustomVariable($id = 4, $name = 'Status user', $value = 'Loggedin', $scope = 'page');
  75. if ($this->useEscapedQuotes) {
  76. $lookingAtProfile = 'looking at &quot;profile page&quot;';
  77. } else {
  78. $lookingAtProfile = 'looking at profile page';
  79. }
  80. $visitorA->setCustomVariable($id = 5, $name = 'Status user', $value = $lookingAtProfile, $scope = 'page');
  81. self::checkResponse($visitorA->doTrackPageView('Profile page'));
  82. $visitorA->setCustomVariable($id = 2, $name = 'SET WITH EMPTY VALUE', $value = '');
  83. $visitorA->setCustomVariable($id = 1, $name = 'Language', $value = 'FR', $scope = 'page');
  84. $visitorA->setCustomVariable($id = 2, $name = 'SET WITH EMPTY VALUE PAGE SCOPE', $value = '', $scope = 'page');
  85. $visitorA->setCustomVariable($id = 4, $name = 'Status user', $value = "looking at \"profile page\"", $scope = 'page');
  86. $visitorA->setCustomVariable($id = 3, $name = 'Value will be VERY long and truncated', $value = 'abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----abcdefghijklmnopqrstuvwxyz----');
  87. self::checkResponse($visitorA->doTrackPageView('Profile page for user *_)%'));
  88. self::checkResponse($visitorA->doTrackGoal($idGoal));
  89. if ($this->doExtraQuoteTests) {
  90. $visitorA->setCustomVariable($id = 2, $name = 'var1', $value = 'looking at "profile page"',
  91. $scope = 'page');
  92. $visitorA->setCustomVariable($id = 3, $name = 'var2', $value = '\'looking at "\profile page"\'',
  93. $scope = 'page');
  94. $visitorA->setCustomVariable($id = 4, $name = 'var3', $value = '\\looking at "\profile page"\\',
  95. $scope = 'page');
  96. self::checkResponse($visitorA->doTrackPageView('Concurrent page views'));
  97. }
  98. // -
  99. // Second new visitor on Idsite 1: one page view
  100. $visitorB = self::getTracker($idSite, $dateTime, $defaultInit = true);
  101. if (!empty($this->visitorId)) {
  102. $visitorB->setVisitorId($this->visitorId);
  103. }
  104. $visitorB->setUrlReferrer('');
  105. $attribution = array(
  106. ' CAMPAIGN NAME -%20YEAH! ',
  107. ' CAMPAIGN%20KEYWORD - RIGHT... ',
  108. 1302306504,
  109. 'http://www.example.org/test/really?q=yes'
  110. );
  111. $visitorB->setAttributionInfo(json_encode($attribution));
  112. $visitorB->setResolution($this->resolutionWidthToUse, $this->resolutionHeightToUse);
  113. $visitorB->setUserAgent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6');
  114. $visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(1)->getDatetime());
  115. $visitorB->setCustomVariable($id = 1, $name = 'VisitorType', $value = 'LoggedOut');
  116. $visitorB->setCustomVariable($id = 2, $name = 'Othercustom value which should be truncated abcdefghijklmnopqrstuvwxyz', $value = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz');
  117. $visitorB->setCustomVariable($id = -2, $name = 'not tracked', $value = 'not tracked');
  118. $visitorB->setCustomVariable($id = 6, $name = 'not tracked', $value = 'not tracked');
  119. $visitorB->setCustomVariable($id = 6, $name = array('not tracked'), $value = 'not tracked');
  120. $visitorB->setUrl('http://example.org/homepage');
  121. self::checkResponse($visitorB->doTrackGoal($idGoal, 1000));
  122. $visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(1.1)->getDatetime());
  123. self::checkResponse($visitorB->doTrackPageView('Homepage'));
  124. // DIFFERENT test -
  125. // testing that starting the visit with an outlink works (doesn't trigger errors)
  126. $visitorB->setForceVisitDateTime(Date::factory($dateTime)->addHour(2)->getDatetime());
  127. self::checkResponse($visitorB->doTrackAction('http://test.com', 'link'));
  128. }
  129. }