Please reach out to MetaRouter before testing or implementing this library.


This library lets you record analytics data from your PHP code. Once installed, the requests will be sent to the MetaRouter platform and then sent to the integrations you have configured for your pipeline that is connected to your PHP events.

You can use the PHP library in your web server controller code. It is built for high performance and uses and internal queue to make all calls non-blocking and fast. It will batch messages and flush asynchronously to the MetaRouter platform.

Clone the PHP Integration & Add Code

Clone the PHP integration into your application directory.

Please reach out to MetaRouter for the library link to clone, as it will be changing soon.

Then, add the following to your code to load in the library.

require_once("reach out to MetaRouter for the path URL");

Initialize the module

Initialize the module with your pipeline's writeKey:

class_alias('MetaRouter', 'Analytics');
Segment::init("YOUR_WRITEKEY", array(
  "host" => "YOUR_HOST_URL"

After this initialization, you have a ready-to-use instance with all event methods built in.

Using Composer to Install the PHP Library

To add the MetaRouter PHP library to a PHP app using composer:

In composer.json, add the GitLab URL to the below repositories section and then require the library as usual.

Note: you need the ‘dev-‘ prefix on the version in the require section as noted in the composer docs.

  "repositories": [
      "type": "vcs",
      "url": "Reach out to MetaRouter for this URL"

  "require": {
    "metarouter/analytics-php": "dev-master"

Required SDK Edits

In the above SDK, you will need to include your custom Host URL. Instructions for setting up your DNS, which includes the Host URL that you will insert into your SDK setup, can be found here. Your writeKey will indicate the specific pipeline you would like to feed your events into.

Event Methods in PHP

Check out the below event methods and their use cases to determine the events that you need to trigger. We have also included examples of how you would call specific objects in PHP. More information on event methods can be found here.


The identify method helps you associate your users and their actions to a unique and recognizable userID and any optional traits that you know about them. We recommend calling an identify a single time - when the user's account is first created and only again when their traits change.

         "userId" => "1234qwerty",
         "traits" => array(
         "name" => "Arthur Dent",
         "email" => "[email protected]",
         "hasTowel" => True,)


To get to a more complete event tracking analytics setup, you can add a track call to your website. This will tell MetaRouter which actions you are performing on your site. With track, each user action triggers an event, which can also have associated properties.

        "userId" => "1234qwerty",
        "event" => "Added File",
        "properties" => array(
        "fileTitle" => "Life, the Universe, and Everything",
        "fileSize" => "42kb",
        "fileType" => "PDF")


The page method allows you to record page views on your website. It also allows you to pass additional information about the pages your users are viewing.

        "userId" => "1234qwerty",
        "section" => "Blog",
        "name" => "10 Questions with Marvin, the clinically depressed robot",
        "properties" => array(
        "referrer" => "")


The group method associates an identified user with a company, organization, project, etc.

        "userId" => "1234qwerty",
        "groupId" => "5678dvorak",
        "traits" => array(
        "name" => "The Hitchhikers",
        "relativePosition" => "[39.1000° N, 84.5167° W]")