Coder Social home page Coder Social logo

piwik-wrap's Introduction

piwik-wrap

A Promise-based wrapper for the Piwik JavaScript Tracking Client providing an enjoyable API

piwik-wrap wraps the Piwik JavaScript Tracking Client and provides an easy to use API for non- and Single Page Applications without polluting global namespace. All JavaScript Tracking Client methods are directly accessible through piwik-wrap.

usage

.init()

Before any other call Piwik must be initialized.

// client bootstrapping

import Piwik from "piwik-wrap";

// ... some other imports and declarations

Piwik.init("https://my.piwik-instance.com", siteId);

.loadScript()

.loadScript() returns a Promise and initializes the Promise-chain.

Piwik.init("https://my.piwik-instance.com", siteId)
  .loadScript()
  .catch((err) => console.error(err));

.then()

// e.g. in a Controller

var Piwik = require("piwik-wrap");

// ...

Piwik
    .init("https://my.piwik-instance.com", siteId)
    .loadScript()
    .then(() => Piwik.setDocumentTitle(document.title)
    .then(() => Piwik.setCustomUrl(document.location.href))
    .then(() => Piwik.trackPageView());

.queue()

It is also possible to use .queue() if you are unfamiliar with Promises.

// client bootstrapping

import Piwik from "piwik-wrap";

// ...

Piwik.init("https://my.piwik-instance.com", siteId).loadScript();

// app.js

class App {
  // ...

  changePage() {
    Piwik.queue("Piwik.setDocumentTitle", document.title)
      .queue("setCustomUrl", "document.location.href")
      .queue("trackPageView");
  }

  // ...
}

.p

// client bootstrapping

import Piwik from "piwik-wrap";

// ...

Piwik.init("https://my.piwik-instance.com", siteId).loadScript();

// PageA.js

class Page {

    // ...

    initTracking() {
        const link = document.querySelector("#trackThisLink");

        link.addEventListener("click", (e) => {
            Piwik.p.then(() => Piwik.trackLink(e.target.getAttribute("href") "linkType");
        }, false)
    }

    // ...

}

// PageComponent.js

import React from "react";
import Piwik frtom "piwik-wrap";

const PageComponent = React.createClass({
    // ...

    componentDidMount() {
        Piwik.p
            .then(() => Piwik.setDocumentTitle(document.title))
            .then(() => Piwik.setCustomUrl(document.location.href))
            .then(() => Piwik.trackPageView());
    }

    // ...

});

Piwik-API-Reference

Check: JavaScript Tracking Client.


Sponsors

piwik-wrap's People

Contributors

topa avatar jannikkeye avatar rutsky avatar

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.