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

Google Bundle Laravel Package

drymek/google-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Clone the submodule into your project:

    git submodule add git@github.com:drymek/GoogleBundle.git src/AntiMattr/GoogleBundle
    

    Register the bundle in app/Kernel.php:

    protected $bundles = [
        // ...
        new AntiMattr\GoogleBundle\GoogleBundle(),
    ];
    
  2. Basic Configuration Add Google Analytics to config/packages/google.yaml (or config.yml):

    google:
        analytics:
            trackers:
                default:
                    name:      "gaTracker"
                    accountId: "UA-xxxx-x"
                    domain:    ".yourdomain.com"
                    trackPageLoadTime: true
    
  3. First Use Case Include the async tracker in your Twig layout (e.g., base.html.twig):

    {% block head %}
        {{ parent() }}
        {% include "GoogleBundle::Analytics:async.html.twig" %}
    {% endblock %}
    

    Verify the script loads in the browser’s <head> or before </body>.


Implementation Patterns

Core Workflows

  1. Service Injection Access the analytics service via dependency injection:

    use AntiMattr\GoogleBundle\Analytics\AnalyticsService;
    
    public function __construct(AnalyticsService $analytics) {
        $this->analytics = $analytics;
    }
    

    Use it to log custom page views:

    $this->analytics->setCustomPageView('/custom-path');
    
  2. Dynamic Tracker Configuration Define multiple trackers in config/google.yaml:

    google:
        analytics:
            trackers:
                default: { accountId: "UA-123-1" }
                secondary: { accountId: "UA-456-2", domain: ".subdomain.com" }
    

    Reference them in Twig:

    {% include "GoogleBundle::Analytics:async.html.twig" with {'tracker': 'secondary'} %}
    
  3. Event-Based Tracking Trigger events (e.g., e-commerce) via the service:

    $this->analytics->trackEvent('Category', 'Action', 'Label', 1.00);
    
  4. JavaScript Maps Integration Leverage the fork’s JS Maps support by including the provided Twig template:

    {% include "GoogleBundle::Maps:init.html.twig" %}
    

    Configure maps in config/google.yaml:

    google:
        maps:
            api_key: "YOUR_API_KEY"
            libraries: ["places", "drawing"]
    

Integration Tips

  • Symfony 5+ Compatibility: Use config/packages/google.yaml for modern Symfony apps.
  • Environment-Specific Config: Override settings per environment (e.g., config/google/dev.yaml).
  • Asset Management: Ensure Google’s JS/CSS assets are loaded via Symfony’s asset pipeline (e.g., Webpack Encore).

Gotchas and Tips

Pitfalls

  1. Submodule Updates

    • Manually update the submodule:
      cd src/AntiMattr/GoogleBundle && git pull origin main
      
    • Avoid composer require; this bundle isn’t Composer-friendly.
  2. Twig Template Paths

    • Use double colons (::) for Twig paths (e.g., "GoogleBundle::Analytics:async.html.twig").
    • Ensure the GoogleBundle is registered before Twig templates are rendered.
  3. Domain Mismatches

    • The domain config must match your site’s domain (e.g., .example.com for example.com and sub.example.com).
    • Test with Google’s Tag Assistant to validate tracking.
  4. Async Loading Conflicts

    • If _gaq fails to load, ensure no other analytics scripts (e.g., GA4) are included.
    • Place the async template before </body> for optimal performance.

Debugging

  • Check Console for Errors: Look for GoogleBundle logs in var/log/dev.log.
  • Verify API Keys: Ensure api_key in config/google.yaml is correct for Maps services.
  • Network Tab: Confirm Google’s JS/CSS loads without 404s (e.g., https://maps.googleapis.com/...).

Extension Points

  1. Custom Trackers Extend the AnalyticsService to add methods for your use case:

    // src/Service/CustomAnalytics.php
    class CustomAnalytics extends AnalyticsService {
        public function trackCustomEvent(string $event) {
            $this->queue[] = ['_trackEvent', [$event]];
        }
    }
    

    Bind it in services.yaml:

    services:
        App\Service\CustomAnalytics: '@google.analytics'
    
  2. Override Twig Templates Copy templates from src/AntiMattr/GoogleBundle/Resources/views/ to templates/bundles/google/ to customize them.

  3. Add New Services Fork the bundle and extend GoogleBundle to support additional Google services (e.g., Firebase):

    // src/AntiMattr/GoogleBundle/GoogleBundle.php
    public function build(ContainerBuilder $container) {
        parent::build($container);
        $container->loadFromExtension('google', [
            'firebase' => ['api_key' => '%env(GOOGLE_FIREBASE_KEY)%']
        ]);
    }
    
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity