Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dfp Bundle Laravel Package

dbtlr/dfp-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require dbtlr/dfp-bundle
    

    (Note: The original README uses legacy methods; Composer is now the standard.)

  2. Enable the Bundle: Add to config/bundles.php:

    return [
        // ...
        Dbtlr\DfpBundle\DbtlrDfpBundle::class => ['all' => true],
    ];
    
  3. Configure DFP Credentials: Add to config/packages/dbtlr_dfp.yaml:

    dfp:
        publisher_id: "YOUR_PUBLISHER_ID"
        network_code: "YOUR_NETWORK_CODE"
        oauth2_client_id: "YOUR_CLIENT_ID"
        oauth2_client_secret: "YOUR_CLIENT_SECRET"
        oauth2_refresh_token: "YOUR_REFRESH_TOKEN"
    
  4. First Use Case: Fetch ad tags in a controller:

    use Dbtlr\DfpBundle\Service\DfpService;
    
    class AdController extends AbstractController
    {
        public function getAdTag(DfpService $dfpService)
        {
            $adTag = $dfpService->getAdTag('YOUR_AD_UNIT_CODE');
            return $this->json(['adTag' => $adTag]);
        }
    }
    

Implementation Patterns

Core Workflows

  1. Ad Tag Generation: Use DfpService to fetch ad tags dynamically:

    $dfpService->getAdTag('header-banner', [
        'ad_size' => ['728x90'],
        'output' => 'html'
    ]);
    
  2. Line Item Management: Create/update line items via LineItemManager:

    $lineItem = $dfpService->createLineItem([
        'name' => 'Spring Campaign',
        'targeting' => ['placement' => ['/sports']],
        'creative' => $creativeId
    ]);
    
  3. Event-Based Targeting: Integrate with Symfony events to trigger DFP actions:

    // In a subscriber
    public function onUserLogin(UserLoginEvent $event, DfpService $dfpService)
    {
        $dfpService->updateUserTargeting($event->getUser(), 'user_id');
    }
    

Integration Tips

  • Twig Integration: Pass the DfpService to Twig templates:

    {{ app.service('dfp_service').getAdTag('sidebar-ad') }}
    

    (Register the service in twig.config.php if needed.)

  • Caching: Cache ad tags for performance:

    $adTag = $dfpService->getCachedAdTag('footer-ad', 3600); // Cache for 1 hour
    
  • Async Operations: Use Symfony Messenger for long-running DFP tasks (e.g., bulk line item updates):

    $message = new UpdateDfpLineItemsMessage($lineItems);
    $this->messageBus->dispatch($message);
    

Gotchas and Tips

Common Pitfalls

  1. OAuth2 Token Expiry:

    • The bundle uses refresh tokens, but ensure your oauth2_refresh_token is valid.
    • Debug with:
      bin/console debug:container dfp_service
      
      (Check if Google_Service_Dfp is properly initialized.)
  2. Deprecated Symfony2 Methods:

    • The bundle assumes Symfony 2.x patterns (e.g., deps file). Use Composer and config/bundles.php instead.
    • Override DfpService if you need Symfony 5+ features (e.g., dependency injection).
  3. Rate Limits:

    • DFP has strict rate limits. Batch requests where possible:
      $dfpService->bulkCreateLineItems($lineItems, 10); // Process 10 at a time
      
  4. Network Code Misconfiguration:

    • Ensure network_code matches your DFP network. Use YOUR_PUBLISHER_ID@YOUR_NETWORK_CODE format if required.

Debugging Tips

  • Enable DFP Logging: Add to config/packages/monolog.yaml:

    handlers:
        dfp:
            type: stream
            path: "%kernel.logs_dir%/dfp.log"
            level: debug
            channels: ["dfp"]
    

    Then tag logs in DfpService:

    $this->logger->debug('DFP Operation', ['event' => 'line_item_create']);
    
  • Validate API Responses: Check for Google_Service_Exception and handle gracefully:

    try {
        $dfpService->getAdTag('invalid-unit');
    } catch (Google_Service_Exception $e) {
        $this->logger->error('DFP Error', ['error' => $e->getMessage()]);
        return $this->render('error/dfp.html.twig');
    }
    

Extension Points

  1. Custom Ad Tag Transformers: Extend DfpService to modify ad tags:

    class CustomDfpService extends DfpService
    {
        public function getAdTag($adUnitCode, array $options = [])
        {
            $tag = parent::getAdTag($adUnitCode, $options);
            return str_replace('YOUR_DOMAIN', request()->getHost(), $tag);
        }
    }
    

    Register as a service in config/services.yaml:

    services:
        Dbtlr\DfpBundle\Service\DfpService: '@app.custom_dfp_service'
    
  2. Webhook Listeners: Add a listener for DFP webhook events (e.g., line item approvals):

    class DfpWebhookListener
    {
        public function onDfpWebhook(DfpWebhookEvent $event)
        {
            if ($event->getType() === 'lineItemApproved') {
                // Trigger internal workflow
            }
        }
    }
    
  3. Testing: Mock DfpService in PHPUnit:

    $mockDfpService = $this->createMock(DfpService::class);
    $mockDfpService->method('getAdTag')->willReturn('<div>Mock Ad</div>');
    $this->container->set('dfp_service', $mockDfpService);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
croct/coding-standard
croct/plug-php
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
imbo/imbo-coding-standard
visualbuilder/filament-lottie
servicioslineaonce/starter-kit
atomcoder/laravel-reorderable
irajul/filament-shadcn-theme
agtp/agtp-php
agtp/mod-php
centraldesktop/protobuf-php
trappistes/laravel-custom-fields
splash/sonata-admin
splash/metadata