PHP code example of marshmallow / marketing-data-tracker
1. Go to this page and download the library: Download marshmallow/marketing-data-tracker 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/ */
marshmallow / marketing-data-tracker example snippets
use Marshmallow\MarketingData\Traits\HasMarketingParameters;
class User extends Model
{
use HasMarketingParameters;
// Your model code...
}
// Get all users from Google Ads campaigns
$googleUsers = User::whereNotNull('gclid')->get();
// Group by campaign
$campaignPerformance = User::select('utm_campaign')
->selectRaw('COUNT(*) as users, SUM(conversion_value) as revenue')
->groupBy('utm_campaign')
->get();
// Platform breakdown
$platformStats = User::selectRaw('
CASE
WHEN gclid IS NOT NULL THEN "Google Ads"
WHEN fbclid IS NOT NULL THEN "Facebook"
WHEN ttclid IS NOT NULL THEN "TikTok"
ELSE "Other"
END as platform,
COUNT(*) as count
')->groupBy('platform')->get();
use Marshmallow\MarketingData\Events\MarketingDataCreated;
use Marshmallow\MarketingData\Events\MarketingDataUpdated;
use Marshmallow\MarketingData\Events\ConversionTracked;
use Marshmallow\MarketingData\Events\ClickIdDetected;
// Listen to marketing data creation
Event::listen(MarketingDataCreated::class, function ($event) {
// Send to analytics when marketing data is first captured
Analytics::trackAcquisition($event->model, $event->getCreatedData());
});
// Listen to click ID detection
Event::listen(ClickIdDetected::class, function ($event) {
// Store click ID for server-side conversion tracking
if ($event->isGoogleClickId()) {
Redis::setex("gclid:{$event->clickId}", 3600, $event->getSessionData());
}
});
// Listen to conversions
Event::listen(ConversionTracked::class, function ($event) {
// Send conversion data to multiple platforms
if ($event->hasValue()) {
GoogleAds::trackConversion($event->getRevenueAttribution());
FacebookConversions::track($event->getConversionData());
}
});
// Listen to marketing data updates
Event::listen(MarketingDataUpdated::class, function ($event) {
if ($event->hasClickIdChanges()) {
// Handle click ID changes
logger()->info('Click ID changed', $event->getClickIdChanges());
}
});
use Marshmallow\MarketingData\Services\CookieManager;
$cookieManager = app(CookieManager::class);
// Get trackable cookies with consent filtering
$cookies = $cookieManager->getCookieValues($request);
// Check consent for specific groups
if ($cookieManager->isTrackingAllowed('advertising')) {
// Track advertising cookies
}
// Wildcard cookie matching
$matches = $cookieManager->matchWildcardCookies(
['_ga_123', '_ga_456', '_fbp'],
['_ga*']
); // Returns: ['_ga_123', '_ga_456']
use Marshmallow\MarketingData\Services\PlatformManager;
$platformManager = app(PlatformManager::class);
// Get enabled platforms
$platforms = $platformManager->getEnabledPlatforms();
// Get platform-specific parameters
$googleParams = $platformManager->getPlatformParameters('google_ads');
// Get all tracking parameters across platforms
$allParams = $platformManager->getAllTrackingParameters();
// Wildcard pattern matching
$matches = $platformManager->matchWildcardPatterns(
['utm_source', 'utm_campaign', 'gclid'],
['utm_*']
); // Returns: ['utm_source', 'utm_campaign']
// v1 - Still works
$clickId = $user->primary_click_id;
// v2 - Enhanced with priority and extraction
$googleClickId = $user->getPrimaryGoogleClickId(); // gclid > wbraid > gbraid
use Marshmallow\MarketingData\Traits\HasMarketingParameters;
use Marshmallow\MarketingData\Traits\TracksConversions;
use Marshmallow\MarketingData\Traits\TracksEcommerceEvents;
class Order extends Model
{
use HasMarketingParameters, TracksConversions, TracksEcommerceEvents;
// Your existing code remains unchanged
}
// New functionality available
$order->trackPurchase('TXN-123', $products, 299.99, 'EUR');
$order->trackConversion('purchase', 299.99);
use Marshmallow\MarketingData\Builders\MarketingUrlBuilder;
// Build campaign URLs with platform-specific parameters
$url = MarketingUrlBuilder::googleAds('https://example.com', 'summer-sale')
->withGoogleValueTrack()
->build();
// Or use the fluent interface
$url = MarketingUrlBuilder::make('https://example.com')
->withUTM('google', 'cpc', 'campaign')
->withPlatform('google_ads', ['mm_keyword' => 'shoes'])
->build();