This is the next post of our blog series where we introduce the capabilities of the Matomo (Piwik) platform (our previous post was How to publish your plugin or theme on the Matomo Marketplace). This time you’ll learn how to create a new command. For this tutorial you will need to have basic knowledge of PHP.

What is a command?

A command can execute any task on the command line. Matomo (Piwik) offers currently about 50 commands via the Matomo Console. These commands let you start the archiver, change the number of available custom variables, enable the developer mode, clear caches, run tests and more. You could write your own command to sync users or websites with another system for instance.

Getting started

In this series of posts, we assume that you have already set up your development environment. If not, visit the Matomo (Piwik) Developer Zone where you’ll find the tutorial Setting up Matomo.

To summarize the things you have to do to get setup:

  • Install Matomo (for instance via git).
  • Activate the developer mode: ./console development:enable --full.
  • Generate a plugin: ./console generate:plugin --name="MyCommandPlugin". There should now be a folder plugins/MyCommandPlugin.
  • And activate the created plugin under Settings => Plugins.

Let’s start creating a command

We start by using the Matomo (Piwik) Console to create a new command. As you can see there is even a command that lets you easily create a new command:

./console generate:command

The command will ask you to enter the name of the plugin the created command should belong to. I will simply use the above chosen plugin name “MyCommandPlugin”. It will ask you for a command name as well. I will use “SyncUsers” in this example. There should now be a file plugins/MyCommandPlugin/Commands/Syncusers.php which contains already an example to get you started easily:

class Syncusers extends ConsoleCommand
{
    protected function configure()
    {
        $this->setName('mycommandplugin:syncusers');
        $this->setDescription('MyCommandPlugin');
        $this->addOption('name', null, InputOption::VALUE_REQUIRED, 'Your name:');
    }

    /**
     * Execute command like: ./console mycommandplugin:syncusers --name="The Matomo Team"
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $name    = $input->getOption('name');

        $message = sprintf('Syncusers: %s', $name);

        $output->writeln($message);
    }
}

Any command that is placed in the “Commands” folder of your plugin will be available on the command line automatically. Therefore, the newly created command can now be executed via ./console mycommandplugin:syncusers --name="The Matomo (Piwik) Team".

The code template explained

protected function configure()
{
    $this->setName('mycommandplugin:checkdatabase');
    $this->setDescription('MyCommandPlugin');
    $this->addOption('name', null, InputOption::VALUE_REQUIRED, 'Your name:');
}

As the name says the method configure lets you configure your command. You can define the name and description of your command as well as all the options and arguments you expect when executing it.

protected function execute(InputInterface $input, OutputInterface $output)
{
    $name    = $input->getOption('name');
    $message = sprintf('Syncusers: %s', $name);
    $output->writeln($message);
}

The actual task is defined in the execute method. There you can access any option or argument that was defined on the command line via $input and write anything to the console via $output argument.

In case anything went wrong during the execution you should throw an exception to make sure the user will get a useful error message. Throwing an exception when an error occurs will make sure the command does exit with a status code different than 0 which can sometimes be important.

Advanced features

The Matomo (Piwik) Console is based on the powerful Symfony Console component. For instance you can ask a user for any interactive input, you can use different output color schemes and much more. If you are interested in learning more all those features have a look at the Symfony console website.

How to test a command

After you have created a command you are surely wondering how to test it. Ideally, the actual command is quite short as it acts like a controller. It should only receive the input values, execute the task by calling a method of another class and output any useful information. This allows you to easily create a unit or integration test for the classes behind the command. We will cover this topic in one of our future blog posts. Just one hint: You can use another command ./console generate:test to create a test. If you want to know how to test a command have a look at the Testing Commands documentation.

Publishing your Plugin on the Marketplace

In case you want to share your commands with other Matomo (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 and best practices when publishing a plugin.

Isn’t it easy to create a command? We never even created a file! If you have any feedback regarding our APIs or our guides in the Developer Zone feel free to send it to us.


Thomas Steur

Thomas is a Lead developer for Matomo. He created and maintains our Matomo Mobile app used by dozens of thousands users every week. Thomas has a passion for making Matomo mobile better and improving the Matomo platform by providing simple APIs and clean code.


Any questions?

Many answers and more information about Matomo you can find here:

We are social

Follow us: