Les articles publiés sur le site
-
How to create a scheduled task – Introducing the Piwik Platform
28 août 2014, par Thomas Steur — DevelopmentThis is the next post of our blog series where we introduce the capabilities of the Piwik platform (our previous post was How to create a custom theme in Piwik). This time you’ll learn how to execute scheduled tasks in the background, for instance sending a daily email. For this tutorial you will need to have basic knowledge of PHP.
What can you do with scheduled tasks?
Scheduled tasks let you execute tasks regularly (hourly, weekly, …). For instance you can:
- create and send custom reports or summaries
- sync users and websites with other systems
- clear any caches
- import third-party data into Piwik
- monitor your Piwik instance
- execute any other task you can think of
Getting started
In this series of posts, we assume that you have already set up your development environment. If not, visit the Piwik Developer Zone where you’ll find the tutorial Setting up Piwik.
To summarize the things you have to do to get setup:
- Install Piwik (for instance via git).
- Activate the developer mode:
./console development:enable --full
. - Generate a plugin:
./console generate:plugin --name="MyTasksPlugin"
. There should now be a folderplugins/MyTasksPlugin
. - And activate the created plugin under Settings => Plugins.
Let’s start creating a scheduled task
We start by using the Piwik Console to create a tasks template:
./console generate:scheduledtask
The command will ask you to enter the name of the plugin the task should belong to. I will simply use the above generated plugin name “MyTasksPlugin”. There should now be a file
plugins/MyTasksPlugin/Tasks.php
which contains some examples to get you started easily:class Tasks extends \Piwik\Plugin\Tasks { public function schedule() { $this->hourly('myTask'); // method will be executed once every hour $this->daily('myTask'); // method will be executed once every day $this->weekly('myTask'); // method will be executed once every week $this->monthly('myTask'); // method will be executed once every month // pass a parameter to the task $this->weekly('myTaskWithParam', 'anystring'); // specify a different priority $this->monthly('myTask', null, self::LOWEST_PRIORITY); $this->monthly('myTaskWithParam', 'anystring', self::HIGH_PRIORITY); } public function myTask() { // do something } public function myTaskWithParam($param) { // do something } }
A simple example
As you can see in the generated template you can execute tasks hourly, daily, weekly and monthly by registering a method which represents the actual task:
public function schedule() { // register method remindMeToLogIn to be executed once every day $this->daily('remindMeToLogIn'); } public function remindMeToLogIn() { $mail = new \Piwik\Mail(); $mail->addTo('me@example.com'); $mail->setSubject('Check stats'); $mail->setBodyText('Log into your Piwik instance and check your stats!'); $mail->send(); }
This example sends you an email once a day to remind you to log into your Piwik daily. The Piwik platform makes sure to execute the method
remindMeToLogIn
exactly once every day.How to pass a parameter to a task
Sometimes you want to pass a parameter to a task method. This is useful if you want to register for instance one task for each user or for each website. You can achieve this by specifying a second parameter when registering the method to execute.
public function schedule() { foreach (\Piwik\Site::getSites() as $site) { // create one task for each site and pass the URL of each site to the task $this->hourly('pingSite', $site['main_url']); } } public function pingSite($siteMainUrl) { file_get_contents($siteMainUrl); }
How to test scheduled tasks
After you have created your task you are surely wondering how to test it. First, you should write a unit or integration test which we will cover in one of our future blog posts. Just one hint: You can use the command
./console generate:test
to create a test. To manually execute all scheduled tasks you can execute the API methodCoreAdminHome.runScheduledTasks
by opening the following URL in your browser:http://piwik.example.com/index.php?module=API&method=CoreAdminHome.runScheduledTasks&token_auth=YOUR_API_TOKEN
Don’t forget to replace the domain and the token_auth URL parameter.
There is one problem with executing the scheduled tasks: The platform makes sure they will be executed only once an hour, a day, etc. This means you can’t simply reload the URL and test the method again and again as you would have to wait for the next hour or day. The proper solution is to set the constant
DEBUG_FORCE_SCHEDULED_TASKS
to true within the file Core/TaskScheduler.php. Don’t forget to set it back to false again once you have finished testing it.Starting from Piwik 2.6.0 you can alternatively execute the following command:
./console core:run-scheduled-tasks --force --token-auth=YOUR_TOKEN_AUTH
The option “–force” will make sure to execute even tasks that are not due to run at this time. So you won’t have to modify any files.
Which tasks are registered and when is the next execution time of my task?
The TasksTimetable plugin from the Marketplace can answer this question for you. Simply install and activate the plugin with one click by going to Settings => Marketplace => Get new functionality. It’ll add a new admin menu item under Settings named Scheduled Tasks.
Publishing your Plugin on the Marketplace
In case you want to share your task(s) with other Piwik users you can do this by pushing your plugin to a public GitHub repository and creating a tag. Easy as that. Read more about how to distribute a plugin.
Advanced features
Isn’t it easy to create scheduled tasks? We never even created a file! Of course, based on our API design principle “The complexity of our API should never exceed the complexity of your use case.” you can accomplish more if you want. For instance, you can define priorities, you can directly register methods from different objects and classes, you can specify at which time of a day a task should run and more.
Would you like to know more about tasks? Go to our Tasks class reference in the Piwik Developer Zone.
If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.
-
How to create a scheduled task – Introducing the Piwik Platform
28 août 2014, par Thomas Steur — DevelopmentThis is the next post of our blog series where we introduce the capabilities of the Piwik platform (our previous post was How to create a custom theme in Piwik). This time you’ll learn how to execute scheduled tasks in the background, for instance sending a daily email. For this tutorial you will need to have basic knowledge of PHP.
What can you do with scheduled tasks?
Scheduled tasks let you execute tasks regularly (hourly, weekly, …). For instance you can:
- create and send custom reports or summaries
- sync users and websites with other systems
- clear any caches
- import third-party data into Piwik
- monitor your Piwik instance
- execute any other task you can think of
Getting started
In this series of posts, we assume that you have already set up your development environment. If not, visit the Piwik Developer Zone where you’ll find the tutorial Setting up Piwik.
To summarize the things you have to do to get setup:
- Install Piwik (for instance via git).
- Activate the developer mode:
./console development:enable --full
. - Generate a plugin:
./console generate:plugin --name="MyTasksPlugin"
. There should now be a folderplugins/MyTasksPlugin
. - And activate the created plugin under Settings => Plugins.
Let’s start creating a scheduled task
We start by using the Piwik Console to create a tasks template:
./console generate:scheduledtask
The command will ask you to enter the name of the plugin the task should belong to. I will simply use the above generated plugin name “MyTasksPlugin”. There should now be a file
plugins/MyTasksPlugin/Tasks.php
which contains some examples to get you started easily:class Tasks extends \Piwik\Plugin\Tasks { public function schedule() { $this->hourly('myTask'); // method will be executed once every hour $this->daily('myTask'); // method will be executed once every day $this->weekly('myTask'); // method will be executed once every week $this->monthly('myTask'); // method will be executed once every month // pass a parameter to the task $this->weekly('myTaskWithParam', 'anystring'); // specify a different priority $this->monthly('myTask', null, self::LOWEST_PRIORITY); $this->monthly('myTaskWithParam', 'anystring', self::HIGH_PRIORITY); } public function myTask() { // do something } public function myTaskWithParam($param) { // do something } }
A simple example
As you can see in the generated template you can execute tasks hourly, daily, weekly and monthly by registering a method which represents the actual task:
public function schedule() { // register method remindMeToLogIn to be executed once every day $this->daily('remindMeToLogIn'); } public function remindMeToLogIn() { $mail = new \Piwik\Mail(); $mail->addTo('me@example.com'); $mail->setSubject('Check stats'); $mail->setBodyText('Log into your Piwik instance and check your stats!'); $mail->send(); }
This example sends you an email once a day to remind you to log into your Piwik daily. The Piwik platform makes sure to execute the method
remindMeToLogIn
exactly once every day.How to pass a parameter to a task
Sometimes you want to pass a parameter to a task method. This is useful if you want to register for instance one task for each user or for each website. You can achieve this by specifying a second parameter when registering the method to execute.
public function schedule() { foreach (\Piwik\Site::getSites() as $site) { // create one task for each site and pass the URL of each site to the task $this->hourly('pingSite', $site['main_url']); } } public function pingSite($siteMainUrl) { file_get_contents($siteMainUrl); }
How to test scheduled tasks
After you have created your task you are surely wondering how to test it. First, you should write a unit or integration test which we will cover in one of our future blog posts. Just one hint: You can use the command
./console generate:test
to create a test. To manually execute all scheduled tasks you can execute the API methodCoreAdminHome.runScheduledTasks
by opening the following URL in your browser:http://piwik.example.com/index.php?module=API&method=CoreAdminHome.runScheduledTasks&token_auth=YOUR_API_TOKEN
Don’t forget to replace the domain and the token_auth URL parameter.
There is one problem with executing the scheduled tasks: The platform makes sure they will be executed only once an hour, a day, etc. This means you can’t simply reload the URL and test the method again and again as you would have to wait for the next hour or day. The proper solution is to set the constant
DEBUG_FORCE_SCHEDULED_TASKS
to true within the file Core/TaskScheduler.php. Don’t forget to set it back to false again once you have finished testing it.Starting from Piwik 2.6.0 you can alternatively execute the following command:
./console core:run-scheduled-tasks --force --token-auth=YOUR_TOKEN_AUTH
The option “–force” will make sure to execute even tasks that are not due to run at this time. So you won’t have to modify any files.
Which tasks are registered and when is the next execution time of my task?
The TasksTimetable plugin from the Marketplace can answer this question for you. Simply install and activate the plugin with one click by going to Settings => Marketplace => Get new functionality. It’ll add a new admin menu item under Settings named Scheduled Tasks.
Publishing your Plugin on the Marketplace
In case you want to share your task(s) with other Piwik users you can do this by pushing your plugin to a public GitHub repository and creating a tag. Easy as that. Read more about how to distribute a plugin.
Advanced features
Isn’t it easy to create scheduled tasks? We never even created a file! Of course, based on our API design principle “The complexity of our API should never exceed the complexity of your use case.” you can accomplish more if you want. For instance, you can define priorities, you can directly register methods from different objects and classes, you can specify at which time of a day a task should run and more.
Would you like to know more about tasks? Go to our Tasks class reference in the Piwik Developer Zone.
If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.
-
How to create a custom theme in Piwik – Introducing the Piwik Platform
23 août 2014, par Thomas Steur — DevelopmentThis is the start of a new blog series where we introduce the capabilities of the Piwik platform. You’ll learn how to write custom plugins & themes, how to use our HTTP APIs and more.
We have been greatly simplifying our APIs over the last year focusing primarily on one design principle:
The complexity of our API should never exceed the complexity of your use case.
In other words, if you have a simple use for our API, we want it to be simple for you to accomplish it. If you have a complex, big, hairy, change-the-world idea, then maybe we can’t make it simple for you to accomplish it, but we want it to be possible.
Over the next weeks and months you will learn what exactly we mean by this and how we accomplished it.
FYI, don’t worry if you’re currently using our APIs, we keep them backwards compatible and we announce breaking changes in our platform changelog.
Getting started
In this series of posts, we assume that you have already set up your development environment. If not, visit the Piwik Developer Zone where you’ll find the tutorial Setting up Piwik.
To summarize the things you have to do to get setup:
- Install Piwik (for instance via git).
- Activate the developer mode:
./console development:enable --full
. - And if you want, generate some test data:
./console visitorgenerator:generate-visits --idsite=1 --limit-fake-visits=600
. This can take a while and requires the VisitorGenerator plugin from the Marketplace.
Let’s start creating our own theme
We start by using the Piwik Console to create a blank theme:
./console generate:theme
The command will ask you to enter a name, description and version number for your theme. I will simply use “CustomTheme” as the name of the theme. There should now be a folder
plugins/CustomTheme
which contains some files to get you started easily.Before we modify our theme, we have to activate it by visiting the Settings => Themes admin page in our Piwik installation, or alternatively by running the command
./console core:plugin activate YourCustomTheme
. If the theme is not activated, we won’t see any changes.Theme Contents
The most important files in our theme are
plugins/CustomTheme/stylesheets/theme.less
,plugins/CustomTheme/stylesheets/_colors.less
andplugins/CustomTheme/stylesheets/_variables.less
:theme.less
is the file that will be included when your theme is activated. In this file you would include other stylesheet files and overwrite CSS styles._colors.less
contains many less variables allowing you to easily change the colors Piwik uses._variables.less
contains currently only one variable to change the font family. More variables will be added in the future. Note: This is a new feature and the file will be only there in case you have installed Piwik using Git or at least Piwik 2.6.0.
Changing the font family
To change the font family simply overwrite the variable
@theme-fontFamily-base: Verdana, sans-serif;
in_variables.less
. That’s it.Changing colors
To change a color, uncomment the less variables of the colors you want to change in
_colors.less
. I will shortly explain some of them. Usually changing only these colors will be enough to adjust Piwik’s look to your corporate design or to create a look that pleases you:@theme-color-brand: #d4291f; // The Piwik red which is for instance used in the menu, it also defines the color of buttons, the little arrows and more @theme-color-brand-contrast: #ffffff; // Contrast color to the Piwik red. Usually you need to change it only in case you define a light brand color. For instance to change the text color of buttons @theme-color-link: #1e93d1; // The link color which is usually a light blue @theme-color-widget-title-text: #0d0d0d; // The text and background color of the header of a widget (Dashboard) @theme-color-widget-title-background: #f2f2f2; @theme-color-menu-contrast-text: #666666; // The text color of a menu item in the reporting sub menu and the admin menu @theme-color-menu-contrast-textActive: #0d0d0d; // The text color of an active menu item @theme-color-menu-contrast-background: #f2f2f2; // The background color of a menu item @graph-colors-data-series[1-8]: #000000; // The different colors used in graphs
Making the change visible
To make a color or font change actually visible when you reload a page in Piwik you will have to delete the compiled CSS file after each change like this:
rm tmp/assets/asset_manager_global_css.css
Publishing your Theme on the Marketplace
In case you want to share your theme with other Piwik users you can do this by pushing your theme to GitHub and creating a tag. Easy as that. Read more about how to distribute a theme.
Advanced features
Isn’t it easy to create a custom theme? All we had to do is to change some less variables. We never even created a file! Of course, based on our API design principle, you can accomplish more if you want. For instance, you can change icons, CSS stylesheets, templates and more.
For further customising your Piwik, you can even change the logo and favicon in the Settings => General settings page.
Would you like to know more about theming? Go to our Theme guide in the Piwik Developer Zone.
If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.
PS: see also this related FAQ: How do I White Label Piwik?
-
How to create a custom theme in Piwik – Introducing the Piwik Platform
23 août 2014, par Thomas Steur — DevelopmentThis is the start of a new blog series where we introduce the capabilities of the Piwik platform. You’ll learn how to write custom plugins & themes, how to use our HTTP APIs and more.
We have been greatly simplifying our APIs over the last year focusing primarily on one design principle:
The complexity of our API should never exceed the complexity of your use case.
In other words, if you have a simple use for our API, we want it to be simple for you to accomplish it. If you have a complex, big, hairy, change-the-world idea, then maybe we can’t make it simple for you to accomplish it, but we want it to be possible.
Over the next weeks and months you will learn what exactly we mean by this and how we accomplished it.
FYI, don’t worry if you’re currently using our APIs, we keep them backwards compatible and we announce breaking changes in our platform changelog.
Getting started
In this series of posts, we assume that you have already set up your development environment. If not, visit the Piwik Developer Zone where you’ll find the tutorial Setting up Piwik.
To summarize the things you have to do to get setup:
- Install Piwik (for instance via git).
- Activate the developer mode:
./console development:enable --full
. - And if you want, generate some test data:
./console visitorgenerator:generate-visits --idsite=1 --limit-fake-visits=600
. This can take a while and requires the VisitorGenerator plugin from the Marketplace.
Let’s start creating our own theme
We start by using the Piwik Console to create a blank theme:
./console generate:theme
The command will ask you to enter a name, description and version number for your theme. I will simply use “CustomTheme” as the name of the theme. There should now be a folder
plugins/CustomTheme
which contains some files to get you started easily.Before we modify our theme, we have to activate it by visiting the Settings => Themes admin page in our Piwik installation, or alternatively by running the command
./console core:plugin activate YourCustomTheme
. If the theme is not activated, we won’t see any changes.Theme Contents
The most important files in our theme are
plugins/CustomTheme/stylesheets/theme.less
,plugins/CustomTheme/stylesheets/_colors.less
andplugins/CustomTheme/stylesheets/_variables.less
:theme.less
is the file that will be included when your theme is activated. In this file you would include other stylesheet files and overwrite CSS styles._colors.less
contains many less variables allowing you to easily change the colors Piwik uses._variables.less
contains currently only one variable to change the font family. More variables will be added in the future. Note: This is a new feature and the file will be only there in case you have installed Piwik using Git or at least Piwik 2.6.0.
Changing the font family
To change the font family simply overwrite the variable
@theme-fontFamily-base: Verdana, sans-serif;
in_variables.less
. That’s it.Changing colors
To change a color, uncomment the less variables of the colors you want to change in
_colors.less
. I will shortly explain some of them. Usually changing only these colors will be enough to adjust Piwik’s look to your corporate design or to create a look that pleases you:@theme-color-brand: #d4291f; // The Piwik red which is for instance used in the menu, it also defines the color of buttons, the little arrows and more @theme-color-brand-contrast: #ffffff; // Contrast color to the Piwik red. Usually you need to change it only in case you define a light brand color. For instance to change the text color of buttons @theme-color-link: #1e93d1; // The link color which is usually a light blue @theme-color-widget-title-text: #0d0d0d; // The text and background color of the header of a widget (Dashboard) @theme-color-widget-title-background: #f2f2f2; @theme-color-menu-contrast-text: #666666; // The text color of a menu item in the reporting sub menu and the admin menu @theme-color-menu-contrast-textActive: #0d0d0d; // The text color of an active menu item @theme-color-menu-contrast-background: #f2f2f2; // The background color of a menu item @graph-colors-data-series[1-8]: #000000; // The different colors used in graphs
Making the change visible
To make a color or font change actually visible when you reload a page in Piwik you will have to delete the compiled CSS file after each change like this:
rm tmp/assets/asset_manager_global_css.css
Publishing your Theme on the Marketplace
In case you want to share your theme with other Piwik users you can do this by pushing your theme to GitHub and creating a tag. Easy as that. Read more about how to distribute a theme.
Advanced features
Isn’t it easy to create a custom theme? All we had to do is to change some less variables. We never even created a file! Of course, based on our API design principle, you can accomplish more if you want. For instance, you can change icons, CSS stylesheets, templates and more.
For further customising your Piwik, you can even change the logo and favicon in the Settings => General settings page.
Would you like to know more about theming? Go to our Theme guide in the Piwik Developer Zone.
If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.
PS: see also this related FAQ: How do I White Label Piwik?
-
Piwik Mobile 2.1 Beta for Android
14 août 2014, par Thomas Steur — Piwik Mobile ReleasesFour years ago we published the first version of Piwik Mobile! To celebrate this event, the next beta of Piwik Mobile 2 for Android is now available.
Download Piwik Mobile 2.1 Beta for Android 4+
What’s new in Piwik Mobile 2.1?
Over the last weeks we’ve been working on this update which includes several performance and UI improvements, bugfixes and new features:
- Support for Segmentation [#3243] – you can now apply your custom segments in Piwik Mobile
- Better support for Events
- Clickable URL’s in a visitor’s actions list
- Expose a general setting to configure SSL certificate validation [#5282]
- Fixed the Real-Time Map was not working on Android 4.4+ [#4413]
- List of all closed issues
We have also updated the underlying framework for better compatibility with new devices and changed some code to improve performance.
Give us feedback
Start using Piwik Mobile 2.1 now and send us any feedback or create an issue on our new Piwik Mobile 2 Issue Tracker.
While the app should be already quite stable we can improve Piwik Mobile much better with your help!
Download now