Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PiwikJsManipulator.php « TrackingCode « CustomJsTracker « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0fe821c4e62829f865a19d73d440fa32fb749028 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link    http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CustomJsTracker\TrackingCode;

use Piwik\Piwik;

class PiwikJsManipulator
{
    const HOOK = '/*!!! pluginTrackerHook */';
    /**
     * @var string
     */
    private $content;

    /** @var PluginTrackerFiles */
    private $pluginTrackerFiles;

    public function __construct($content, PluginTrackerFiles $pluginTrackerFiles)
    {
        $this->content = $content;
        $this->pluginTrackerFiles = $pluginTrackerFiles;
    }

    public function manipulateContent()
    {
        $files = $this->pluginTrackerFiles->find();

        foreach ($files as $file) {
            $trackerExtension = $this->getSignatureWithContent($file->getName(), $file->getContent());

            // for some reasons it is /*!!! in piwik.js minified and /*!! in js/piwik.js unminified
            $this->content = str_replace(array(self::HOOK, '/*!! pluginTrackerHook */'), self::HOOK . $trackerExtension, $this->content);
        }

        $content = $this->content;

        /**
         * Triggered after the Matomo JavaScript tracker has been generated and shortly before the tracker file
         * is written to disk. You can listen to this event to for example automatically append some code to the JS
         * tracker file.
         *
         * **Example**
         *
         *     function onManipulateJsTracker (&$content) {
         *         $content .= "\nPiwik.DOM.onLoad(function () { console.log('loaded'); });";
         *     }
         *
         * @param string $content the generated JavaScript tracker code
         */
        Piwik::postEvent('CustomMatomoJs.manipulateJsTracker', array(&$content));
        $this->content = $content;

        return $this->content;
    }

    /**
     * @param string $name
     * @param string $content
     * @return string
     */
    private function getSignatureWithContent($name, $content)
    {
        return sprintf(
            "\n\n/* GENERATED: %s */\n%s\n/* END GENERATED: %s */\n",
            $name,
            $content,
            $name
        );
    }

}