PHP code example of moesif / moesif-slim

1. Go to this page and download the library: Download moesif/moesif-slim library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moesif / moesif-slim example snippets



use Slim\App;
use Slim\Factory\AppFactory;
use Moesif\Middleware\MoesifSlim;

// Create App instance
$app = AppFactory::create();

$moesifOptions = [
    'applicationId' => 'Your Moesif Application Id',
];

$app->addErrorMiddleware(true, true, true);

// Add Moesif Middleware
$middleware = new MoesifSlim($moesifOptions);
$app->add($middleware);


// In config/moesif.php

return [
    //
    'applicationId' => 'Your Moesif Application Id',
    'logBody' => true,
];


// In config/moesif.php

$identifyUserId = function($request, $response) {
    // Your custom code that returns a user id string
    return '12345';
};

return [
  //
  'identifyUserId' => $identifyUserId
];


// In config/moesif.php

$identifyCompanyId = function($request, $response) {
    # Your custom code that returns a company id string
    return '67890';
};

return [
  //
  'identifyCompanyId' => $identifyCompanyId
];


// In config/moesif.php

$getMetadata = function($request, $response) {
  return array("foo"=>"Slim Framework example", "boo"=>"custom data");
};

return [
  //
  'getMetadata' => $getMetadata
];


// In config/moesif.php

$maskRequestHeaders = function($headers) {
    $headers['password'] = '****';
    return $headers;
};

return [
  //
  'maskRequestHeaders' => $maskRequestHeaders
];


// In config/moesif.php

$maskRequestBody = function($body) {
    // remove any sensitive information.
    $body['password'] = '****';
    return $body;
};

return [
  //
  'maskRequestBody' => $maskRequestBody
];

use Moesif\Middleware\MoesifSlim;

// Only userId is nt to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#users for campaign schema
// metadata can be any custom object
$user = array(
    "user_id" => "12345",
    "company_id" => "67890", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "[email protected]",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "[email protected]"
        )
    )
);

$middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']);
$middleware->updateUser($user);

use Moesif\Middleware\MoesifSlim;

$userA = array(
    "user_id" => "12345",
    "company_id" => "67890", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "[email protected]",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "[email protected]"
        )
    )
);

$userB = array(
    "user_id" => "1234",
    "company_id" => "6789", // If set, associate user with a company object
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "email" => "[email protected]",
        "first_name" => "John",
        "last_name" => "Doe",
        "title" => "Software Engineer",
        "sales_info" => array(
            "stage" => "Customer",
            "lifetime_value" => 24000,
            "account_owner" => "[email protected]"
        )
    )
);

$users = array($userA, $userB);

$middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']);
$middleware->updateUsersBatch($users);

use Moesif\Middleware\MoesifSlim;

// Only companyId is to track ROI of acquisition channels
// See https://www.moesif.com/docs/api#update-a-company for campaign schema
// metadata can be any custom object
$company = array(
    "company_id" => "67890",
    "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info 
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    )
);

$middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']);
$middleware->updateCompany($company);

use Moesif\Middleware\MoesifSlim;

$companyA = array(
    "company_id" => "67890",
    "company_domain" => "acmeinc.com", // If domain is set, Moesif will enrich your profiles with publicly available info 
    "campaign" => array(
        "utm_source" => "google",
        "utm_medium" => "cpc",
        "utm_campaign" => "adwords",
        "utm_term" => "api+tooling",
        "utm_content" => "landing"
    ),
    "metadata" => array(
        "org_name" => "Acme, Inc",
        "plan_name" => "Free",
        "deal_stage" => "Lead",
        "mrr" => 24000,
        "demographics" => array(
            "alexa_ranking" => 500000,
            "employee_count" => 47
        )
    )
);

$companies = array($companyA);

$middleware = new MoesifSlim(['applicationId' => 'Your Moesif Application Id']);
$middleware->updateCompaniesBatch($companies);