Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the JS tracking code instead of the dashboard while no visit is tracked #7365

Merged
merged 13 commits into from
Mar 12, 2015
15 changes: 15 additions & 0 deletions core/Tracker/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,21 @@ public function findVisitor($idSite, $configId, $idVisitor, $fieldsToRead, $numC
return $visitRow;
}

/**
* Returns true if the site doesn't have log data.
*
* @param int $siteId
* @return bool
*/
public function isSiteEmpty($siteId)
{
$sql = sprintf('SELECT idsite FROM %s WHERE idsite = ? limit 1', Common::prefixTable('log_visit'));

$result = \Piwik\Db::fetchOne($sql, array($siteId));

return $result == null;
}

private function visitFieldsToQuery($valuesToUpdate)
{
$updateParts = array();
Expand Down
17 changes: 17 additions & 0 deletions plugins/SitesManager/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Piwik\SettingsPiwik;
use Piwik\Site;
use Piwik\Tracker\TrackerCodeGenerator;
use Piwik\Url;
use Piwik\View;

/**
Expand Down Expand Up @@ -118,4 +119,20 @@ function downloadPiwikTracker()
header('Content-Disposition: attachment; filename="' . $filename . '"');
return file_get_contents($path . $filename);
}

public function siteWithoutData()
{
$javascriptGenerator = new TrackerCodeGenerator();
$piwikUrl = Url::getCurrentUrlWithoutFileName();

return $this->renderTemplate('siteWithoutData', array(
'siteName' => $this->site->getName(),
'trackingHelp' => $this->renderTemplate('_displayJavascriptCode', array(
'displaySiteName' => $this->site->getName(),
'jsTag' => $javascriptGenerator->generate($this->idSite, $piwikUrl),
'idSite' => $this->idSite,
'piwikUrl' => $piwikUrl,
)),
));
}
}
25 changes: 24 additions & 1 deletion plugins/SitesManager/SitesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*
*/
namespace Piwik\Plugins\SitesManager;

use Piwik\Common;
use Piwik\DataAccess\ArchiveInvalidator;
use Piwik\Db;
use Piwik\Tracker\Cache;
use Piwik\Tracker\Model as TrackerModel;

/**
*
Expand All @@ -29,10 +33,29 @@ public function getListHooksRegistered()
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'Tracker.Cache.getSiteAttributes' => 'recordWebsiteDataInCache',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'SitesManager.deleteSite.end' => 'onSiteDeleted'
'SitesManager.deleteSite.end' => 'onSiteDeleted',
'Request.dispatch' => 'redirectDashboardToWelcomePage',
);
}

public function redirectDashboardToWelcomePage(&$module, &$action)
{
if ($module !== 'CoreHome' || $action !== 'index') {
return;
}

$siteId = Common::getRequestVar('idSite', false, 'int');
if (!$siteId) {
return;
}

$trackerModel = new TrackerModel();
if ($trackerModel->isSiteEmpty($siteId)) {
$module = 'SitesManager';
$action = 'siteWithoutData';
}
}

public function onSiteDeleted($idSite)
{
// we do not delete logs here on purpose (you can run these queries on the log_ tables to delete all data)
Expand Down
3 changes: 3 additions & 0 deletions plugins/SitesManager/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
"ShowTrackingTag": "View Tracking code",
"Sites": "Websites",
"SiteSearchUse": "You can use Piwik to track and report what visitors are searching in your website's internal search engine.",
"SiteWithoutDataTitle": "No data has been recorded yet",
"SiteWithoutDataDescription": "No analytics data has been tracked for this website yet.",
"SiteWithoutDataSetupTracking": "Please set up the %1$sJavaScript tracking code%2$s on your website and refresh the page.",
"SuperUserAccessCan": "A user with Super User access can also %s specify global settings%s for new websites.",
"Timezone": "Time zone",
"TrackingSiteSearch": "Tracking Internal Site Search",
Expand Down
18 changes: 17 additions & 1 deletion plugins/SitesManager/stylesheets/SitesManager.less
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ td.editable-site-field:hover {

.link_but:hover > span {
text-decoration: underline;
}
}

.site-without-data {
padding: 15px;
h2 {
border-bottom: 1px solid #ccc;
margin: 25px 0;
padding: 0 0 5px 0;
font-size: 24px;
}
h3 {
margin: 15px 0;
font-size: 18px;
line-height: 24px;
font-weight: normal;
}
}
25 changes: 25 additions & 0 deletions plugins/SitesManager/templates/siteWithoutData.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "dashboard.twig" %}

{% block notification %}{% endblock %}

{% block content %}

{% include "@CoreHome/_siteSelectHeader.twig" %}

<div class="site-without-data">

<h2>{{ 'SitesManager_SiteWithoutDataTitle'|translate }}</h2>

<p>
{{ 'SitesManager_SiteWithoutDataDescription'|translate }}
{{ 'SitesManager_SiteWithoutDataSetupTracking'|translate('<a href="' ~ linkTo({
'module': 'CoreAdminHome',
'action': 'trackingCodeGenerator',
}) ~ '">', '</a>')|raw }}
</p>

{{ trackingHelp|raw }}

</div>

{% endblock %}
2 changes: 2 additions & 0 deletions plugins/SitesManager/tests/UI/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/processed-ui-screenshots
/screenshot-diffs
27 changes: 27 additions & 0 deletions plugins/SitesManager/tests/UI/EmptySite_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Piwik - free/libre analytics platform
*
* Screenshot integration tests.
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

describe("EmptySite", function () {
this.timeout(0);

var generalParams = 'idSite=4&period=day&date=2010-01-03';

before(function () {
testEnvironment.pluginsToLoad = ['SitesManager'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diosmosis is it expected we have to manually load the actual plugin the test is written in? could we improve this to automatically load the plugin being tested (here the UI test is in the SitesManager plugin)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, tests should be run as ./console tests:run-ui --plugin=SitesManager for plugins. howeve, SitesManager is a core plugin, it should always be loaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK I will remove that line then, I just used the UI tests generator and that was added automatically.

testEnvironment.save();
});

it('should show the tracking code if the website has no recorded data', function (done) {
var urlToTest = "?" + generalParams + "&module=CoreHome&action=index";

expect.screenshot('emptySiteDashboard').to.be.captureSelector('.site-without-data', function (page) {
page.load(urlToTest);
}, done);
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/PHPUnit/Integration/Tracker/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Piwik\Common;
use Piwik\Db;
use Piwik\Tests\Fixtures\OneVisitorTwoVisits;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Tracker\Model;

Expand Down Expand Up @@ -111,6 +112,16 @@ public function test_getIdsAction_CorrectlyReturnsLowestIdActions_IfDuplicateIdA
$this->assertEquals($expectedResult, $result);
}

public function test_isSiteEmpty()
{
$this->assertTrue($this->model->isSiteEmpty(1));

$fixture = new OneVisitorTwoVisits();
$fixture->setUp();

$this->assertFalse($this->model->isSiteEmpty(1));
}

private function assertLogActionTableContainsTestAction($idaction)
{
$expectedRows = array(
Expand Down