webfactory/piwik-bundle

Symfony 扩展包,添加了用于 Matomo 跟踪代码的 Twig 函数。

安装次数: 111,241

依赖项: 1

建议者: 0

安全: 0

星标: 39

关注者: 10

分支: 16

开放问题: 4

类型:symfony-bundle

3.3.0 2024-04-22 12:14 UTC

README

Run Tests Coverage Status

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

它包含一个 Twig 函数,可以将跟踪代码插入到您的网站中。此外,您可以通过简单的配置开关将其关闭,这样就不会跟踪您的开发环境。

安装

composer require webfactory/piwik-bundle

并在 app/AppKernel.php 中启用扩展包

<?php
// app/AppKernel.php

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

使用方法

在您的视图中的某个位置,在 </body> 标签之前插入

{{ piwik_code() }}

这将按照 API 参考中所述添加适当的 Matomo 跟踪代码。

配置

您可以在您的 config.yml 中配置扩展包。完整示例

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

添加对 JavaScript 跟踪器 API 的调用

JavaScript 跟踪器 API 提供了多种方法来设置页面名称、跟踪搜索结果、使用自定义变量等。

通用的 piwik() 函数允许您控制 _paq 变量并向其中添加额外的 API 调用。例如,在您的 Twig 模板中,您可以编写

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

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

注意,当您调用 trackSiteSearch 时,这将自动禁用默认的 trackPageView 调用。这是建议的行为。

致谢、版权和许可证

此代码由德国波恩的 webfactory GmbH 编写。我们是一家专注于 PHP(主要Symfony)的软件开发机构。如果您是一名寻求新挑战的开发者,我们愿意听取您的意见!

版权所有 2012 – 2020 webfactory GmbH, 波恩。代码在 MIT 许可证 下发布。