Giter Club home page Giter Club logo

piwik-bundle's Introduction

webfactory Logo WebfactoryPiwikBundle

Run Tests Coverage Status

A Symfony Bundle that helps you to use the Matomo (formerly known as Piwik) Open Analytics Platform with your project.

It contains a Twig function that can insert the tracking code into your website. Plus, you can turn it off with a simple configuration switch so you don't track your dev environment.

Installation

composer require webfactory/piwik-bundle

And enable the bundle in app/AppKernel.php:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Webfactory\Bundle\PiwikBundle\WebfactoryPiwikBundle(),
    );
}

Usage

Somewhere in your views, right before the closing </body> tag, insert

{{ piwik_code() }}

This will add the appropriate Matomo tracking code as described in the API reference.

Configuration

You can configure the bundle in your config.yml. Full Example:

webfactory_piwik:
    # Required, no default. Must be set to the site id found in the Matomo control panel
    site_id: 1
    # Required. no default. Hostname and path to the Matomo host.
    piwik_host: my.piwik.hostname
    # Optional, has default. Usually, you only want to include the tracking code in a production environment
    disabled: '%kernel.debug%'
    # Optional, has default. Path to the tracking script on the host.
    tracker_path: "/js/"
    # Optional, has default. Disable cookies in favor of GDPR
    # https://matomo.org/faq/new-to-piwik/how-do-i-use-matomo-analytics-without-consent-or-cookie-banner/ & https://matomo.org/faq/general/faq_157/
    disable_cookies: true

Add calls to the JavaScript tracker API

The JavaScript tracking API provides a lot of methods for setting the page name, tracking search results, using custom variables and much more.

The generic piwik() function allows you to control the _paq variable and add additional API calls to it. For example, in your Twig template, you can write

    <!-- Somewhere in your HTML, not necessarily at the end -->
    {{ piwik("setDocumentTitle", document.title) }}
    {{ piwik("trackGoal", 1) }}

    <!-- Then, at the end: -->
    {{ piwik_code() }}
    </body>

Note that when you call trackSiteSearch, this will automatically disable the trackPageView call made by default. This is the recommended behaviour.

Credits, Copyright and License

This code was written by webfactory GmbH, Bonn, Germany. We're a software development agency with a focus on PHP (mostly Symfony). If you're a developer looking for new challenges, we'd like to hear from you!

Copyright 2012 โ€“ 2020 webfactory GmbH, Bonn. Code released under the MIT license.

piwik-bundle's People

Contributors

artursvonda avatar bnw avatar bretrzaun avatar fabianschmick avatar genietim avatar maltewunsch avatar matthimatiker avatar mhundeloh avatar mnapoli avatar mpdude avatar relthyg avatar th3mouk avatar tomaszgasior avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

piwik-bundle's Issues

README: Configuration fix

The example documentation in the README should say:
disabled: '%kernel.debug%'
instead of:
disabled: %kernel.debug%

Otherwise Symfony(4) will complain about invalid YAML.

Rename "Symfony2" to "Symfony"

The repository description and the documentation contains "Symfony2". This should be renamed to "Symfony" (which is the new name to say we support version 2 and version 3, everybody in the community does it this way).

Enforce HTTPS for Matomo requests ๐Ÿ”

It would be great if we could have a configuration setting to enforce all Matomo requests be over HTTPS, no โ€“ as it is right now โ€“ HTTP or HTTPS depending on the current scheme.

Support for Symfony 4 / Flex

As this project requires symfony/symfony, which is added as conflicting package for flex, they do not work together. The relevant dependencies of symfony can be added separately to reach support.

Is someone already working on this, or do you mind a pull request?

Deprecation in Symfony 4.3

After update to sf 4.3 I have this deprecation message in tests and profiler:

The "Symfony\Component\Config\Definition\Builder\TreeBuilder::root()" method called for the "webfactory_piwik" configuration is deprecated since Symfony 4.3, pass the root name to the constructor instead.

Add <noscript> to piwik_code()

Piwik FAQ: How do I enable tracking for visitors that have disabled Javascript?

When the visitor disables javascript, there would be no tracking. Piwik suggest to add

<noscript><p><img src="http://piwik-server.com/piwik.php?idsite=1&rec=1" style="border:0" alt="" /></p></noscript>

which does not have the full tracking functionality, but is a good backup plan.
Could I submit a pull request to include this?
Would you prefer to have an argument to the piwik_code function call

{{ piwik_code(noscript=true) }}

Or should it be a configuration parameter? Or anything else?

Support of multiple trackers

I've seen a few cases when people were willing to use multiple trackers in their apps. It would be nice if your repository could support them too in a way. The config would look like this:

webfactory_piwik:
    main:
        site_id: 1
        disabled: %kernel.debug%
        piwik_host: my.piwik.hostname
       tracker_path: "/js/"
    other:
        site_id: 42
        disabled: %kernel.debug%
        piwik_host: example.com
        tracker_path: "/analytics/"

If this ever gets implemented, service names in #7 would have to be made different too:

$tracker1 = $container->get('webfactory_piwik.tracker.main');
$tracker2 = $container->get('webfactory_piwik.tracker.other');

Symfony deprecation

Using Symfony 5.4 we see the following deprecation message when running our tests:
1x: Method "Twig\Extension\ExtensionInterface::getFunctions()" might add "array" as a native return type declaration in the future. Do the same in implementation "Webfactory\Bundle\PiwikBundle\Twig\Extension" now to avoid errors or add an explicit @return annotation to suppress this message.

Should be any easy fix. Do you want a PR for that?

Link with piwik/piwik-php-tracker

In some web apps it may be useful to supplement the javascript tracker with a PHP one. This will help ensure that the statistics are not partially missing because of JS errors in old browsers or very slow networks.

Official implementation of the PHP tracker is in piwik/piwik-php-tracker. It can be easily made available as a service in your bundle. Usage example:

$tracker = $container->get('webfactory_piwik.tracker');
$tracker->doThis();
$tracker->doThat();
$response = new Response();
//...
return $response;

When piwik is disabled in config, the PHP tracker should be too (the service can be replaced with a dummy object having the same API).

Support Twig 2.0

Hello.

The bundle requires twig: ~1.0, which make it impossible to use with Twig 2.0.

The child node "piwik_host" at path "webfactory_piwik" must be configured.

Hello,

Thank you for this project. When trying to set it up for the first time I get the following error:

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!  In ArrayNode.php line 216:
!!                                                                                
!!    The child node "piwik_host" at path "webfactory_piwik" must be configured.  
!!                                                                                
!!  
!!  
Script @auto-scripts was called via post-update-cmd

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

My configuration is located in config/packages/matomo.yml and contains:

webfactory_piwik:
    site_id: 1
    piwik_host: 'example.com'
    disabled: '%kernel.debug%'
    disable_cookies: true

Am I doing something wrong?

Rename "use_cacheable_tracking_script" option

I'd like to suggest renaming the use_cacheable_tracking_script option to something like tracker_uri or so.

The reason is that there are several things you can do with this script (see https://github.com/piwik/piwik/blob/master/js/README.md). Folks might even have custom variants of it and use it as /js/ or /js/index.php.

IMO tracker_uri also is more intention-revealing. It should default to /js/ though :-).

Too bad we've already tagged 1.0 and this is an API break, but I'd accept that and not go for 2.0 because of this.

Current version of piwik-bundle can't be installed onto symfony 6.3

There seems to be some dependency pinning. Here's what i get when I attempt to install it :

# composer require webfactory/piwik-bundle
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update webfactory/piwik-bundle
Loading composer repositories with package information
Restricting packages listed in "symfony/symfony" to "6.3.*"
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires symfony/cache 6.3.*, found symfony/cache[v6.3.0, ..., v6.3.5] but the package is fixed to v6.2.13 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
  Problem 2
    - Root composer.json requires symfony/dotenv 6.3.*, found symfony/dotenv[v6.3.0] but the package is fixed to v6.2.8 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
  Problem 3
    - Root composer.json requires symfony/http-client 6.3.*, found symfony/http-client[v6.3.0, v6.3.1, v6.3.2, v6.3.5] but the package is fixed to v6.2.13 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
  Problem 4
    - Root composer.json requires symfony/mime 6.3.*, found symfony/mime[v6.3.0, v6.3.2, v6.3.3, v6.3.5] but the package is fixed to v6.2.13 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
  Problem 5
    - Root composer.json requires symfony/runtime 6.3.*, found symfony/runtime[v6.3.0, v6.3.1, v6.3.2] but the package is fixed to v6.2.13 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
  Problem 6
    - __root__ is present at version 1.0.0+no-version-set and cannot be modified by Composer
    - __root__ 1.0.0+no-version-set conflicts with symfony/symfony 2.0.6.
    - Root composer.json requires symfony/console 6.3.* -> satisfiable by symfony/symfony[2.0.6].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.
You can also try re-running composer require with an explicit version constraint, e.g. "composer require webfactory/piwik-bundle:*" to figure out if any version is installable, or "composer require webfactory/piwik-bundle:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Wrongly formed URL for the tracker

Hi there, awesome stuff, I've just noticed a tiny issue: the bundle is adding a double // in the URL:

https://demo.piwik.org//js/

It doesn't seem to cause any real problem, but I guess it's best to let you know.

By the way just to be clear, my config doesn't contain any extra / (the host name is just demo.piwik.org), and I haven't customized the tracker_path.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.