Giter Club home page Giter Club logo

piwik-proxy-service-provider's Introduction

PiwikProxyServiceProvider

Build Status

PiwikProxyServiceProvider allows you to quickly create a proxying route for Piwik tracking to hide the location of Piwik's server.

Requirements

The only requirement is to have file_get_contents switched to enable URL fetching. But you can ommit this requirement by providing own implementation of Mach\Silex\Piwik\RemoteContentInterface which is used to execute calls to Piwik's installation. By default it uses file_get_contents but you can provide your own version which will use cURL for instance.

Installation

Add requirement: php composer.phar require mach/piwik-proxy-service-provider:~1.0.

And update it php composer.phar update mach/piwik-proxy-service-provider.

Configuration

Below is a structure of configuration array for the provider, and descriptions for each keys.

{
    "piwik.proxy.url": "<url-to-piwik-installation>",
    "piwik.proxy.token": "<token-with-admin-access-to-sites>",
    "piwik.proxy.timeout": "<timeout-in-seconds-for-request>",
    "piwik.proxy.cache": "<cache-in-seconds-for-js-file>"
}

By default, piwik.proxy.timeout is set to 5, and piwik.proxy.cache to 86400 which gives us a daily cache for requesting javascript tracker.

Usage

To use the provider you have to register it first and provide options:

<?php

use Silex\Application;
use Mach\Silex\Piwik\PiwikProxyServiceProvider;

$app = new Application();
$app->register(new PiwikProxyServiceProvider(), array(
    'piwik.proxy.url' => 'http://piwik.example.com',
    'piwik.proxy.token' => 'xyz',
));

The provider registers a closure under $app['piwik.proxy']. The closure always returns an instance of Symfony\Component\HttpFoundation\Response.

Next step is to create a route (e.g. /tracker) that will serve the JavaScript file and handle tracking requests.

<?php

$app->get('/tracker', function(Request $request) use ($app) {
    return $app['piwik.proxy']($request);
});

To override default settings, you can pass array of options as second argument to the closure:

<?php

$app->get('/tracker/{id}', function(Request $request, $id) use ($app) {
    return $app['piwik.proxy']($request, array(
        'url' => "https://piwik-$id.example.com"
    ));
});

Change your tracking code to:

<!-- Piwik -->
<script type="text/javascript">
    var _paq = _paq || [];
    _paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
    _paq.push(["setCookieDomain", "*.yourdomain.net"]);
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    (function() {
        _paq.push(['setTrackerUrl', '/tracker']);
        _paq.push(['setSiteId', 1]);
        var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
        g.type='text/javascript'; g.async=true; g.defer=true; g.src='/tracker'; s.parentNode.insertBefore(g,s);
    })();
</script>
<noscript><p><img src="/tracker?idsite=1" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->

License

PiwikProxyServiceProvider is licensed under the MIT license.

piwik-proxy-service-provider's People

Contributors

ivoba avatar

Watchers

James Cloos avatar

Forkers

ivoba

piwik-proxy-service-provider's Issues

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.