PHP code example of moesif / moesif-laravel
1. Go to this page and download the library: Download moesif/moesif-laravel 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-laravel example snippets
// In config/app.php
'providers' => [
/*
* Application Service Providers...
*/
Moesif\Middleware\MoesifLaravelServiceProvider::class,
];
// In App/Http/Kernel.php
protected $middleware = [
/*
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*/
\Moesif\Middleware\MoesifLaravel::class,
];
// In App/Http/Kernel.php
protected $middlewareGroups = [
/**
* The application's API route middleware group.
*/
'api' => [
//
\Moesif\Middleware\MoesifLaravel::class,
],
];
// In config/moesif.php
return [
//
'applicationId' => 'Your Moesif Application Id',
'logBody' => true,
];
return [
...
'configClass' => 'MyApp\\MyConfigs\\CustomMoesifConfig',
...
];
namespace MyApp\MyConfigs;
class CustomMoesifConfig
{
public function maskRequestHeaders($headers) {
$headers['header5'] = '';
return $headers;
}
public function maskRequestBody($body) {
return $body;
}
public function maskResponseHeaders($headers) {
$headers['header2'] = 'XXXXXX';
return $headers;
}
public function maskResponseBody($body) {
return $body;
}
public function identifyUserId($request, $response) {
if (is_null($request->user())) {
return null;
} else {
$user = $request->user();
return $user['id'];
}
}
public function identifyCompanyId($request, $response) {
return "67890";
}
public function identifySessionId($request, $response) {
if ($request->hasSession()) {
return $request->session()->getId();
} else {
return null;
}
}
public function getMetadata($request, $response) {
return array("foo"=>"a", "boo"=>"b");
}
public function skip($request, $response) {
$myurl = $request->fullUrl();
if (strpos($myurl, '/health') !== false) {
return true;
}
return false;
}
}
use Moesif\Middleware\MoesifLaravel;
// Only userId is 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 MoesifLaravel();
$middleware->updateUser($user);
use Moesif\Middleware\MoesifLaravel;
$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" => "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] "
)
)
);
$users = array($userA);
$middleware = new MoesifLaravel();
$middleware->updateUsersBatch($users);
use Moesif\Middleware\MoesifLaravel;
// Only companyId is 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 MoesifLaravel();
$middleware->updateCompany($company);
use Moesif\Middleware\MoesifLaravel;
$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 MoesifLaravel();
$middleware->updateCompaniesBatch($companies);
return [
'applicationId' => 'Your Moesif Application Id',
'disableForking' => true,
];
// In config/moesif.php
return [
//
'applicationId' => 'Your Moesif Application Id',
'debug' => true,
];
$identifyUserId = function($request, $response) {
// Your custom code that returns a user id string
$user = $request->user();
if ($request->user()) {
return $user->id;
}
return NULL;
};
return [
...,
'identifyUserId' => $identifyUserId,
]
namespace MyApp\MyConfigs;
class CustomMoesifConfig
{
public function identifyUserId($request, $response) {
if (is_null($request->user())) {
return null;
} else {
$user = $request->user();
return $user['id'];
}
}
// add other methods for closure based configs.
}
return [
...,
'configClass' => 'MyApp\\MyConfigs\\CustomMoesifConfig',
]
bash
$ php artisan vendor:publish --provider="Moesif\Middleware\MoesifLaravelServiceProvider"