Google Tag Manager - v1.0.0

Snippet Renderer

ArtisanPackUI\GoogleTagManager\Snippet\SnippetRenderer is the class behind the @gtmSnippet / @gtmSnippetHead / @gtmSnippetBody Blade directives.

Registered as a singleton — resolve it once and reuse.

use ArtisanPackUI\GoogleTagManager\Snippet\SnippetRenderer;

$renderer = app( SnippetRenderer::class );

Methods

isConfigured(): bool

Whether the snippet will render output. true when snippet.enabled is truthy AND snippet.container_id is a non-empty string.

containerId(): ?string

Returns the configured container ID, or null when unset / empty.

render(): string

Returns the combined head <script> block and body <noscript> fallback separated by a newline. Returns an empty string when isConfigured() is false.

renderHead(): string

Returns just the head <script> block. Meant for the closing </head>. Empty string when unconfigured.

renderBody(): string

Returns just the body <noscript> fallback. Meant for the opening <body>. Empty string when unconfigured.

Escaping

  • The container ID is escaped for JavaScript string interpolation in renderHead() — backslashes, quotes, newlines, and </> are neutralized.
  • The container ID is escaped for HTML in renderBody() — the <noscript> src attribute is safe against attribute-injection payloads.

Not overrideable

The class is not final but its rendered markup is fixed — the standard GTM <script> block. If you need custom markup (for example, a proxied gtm.js URL), either extend the class and rebind it in a service provider, or write your own directive.