gebruederheitz / wp-gtm
A helper to include the Google Tag Manager in your Wordpress
Requires
- php: >=7.3
Requires (Dev)
- phpstan/phpstan: ^1.4
- szepeviktor/phpstan-wordpress: ^1.0
This package is auto-updated.
Last update: 2024-11-09 01:08:03 UTC
README
A helper to include the Google Tag Manager in your Wordpress.
Installation
via composer:
> composer require gebruederheitz/wp-gtm
Make sure you have Composer autoload or an alternative class loader present.
Usage
# functions.php (or controller class) use Gebruederheitz\Wordpress\GoogleTagManager; new GoogleTagManager();
# .env #--------------------------------------------------------------------------------------------------# # GOOGLE TAGMANAGER # # Adds Google Tagmanager snippets # #--------------------------------------------------------------------------------------------------# GTM_CONTAINER_ID=GTM-XXXXX
You can also add parameters to your container ID in order for example to use a staging environment.
If the environment variable is not defined, the script snippet will not be loaded, unless you provide a custom GTM container ID to the constructor.
Passing a container ID at runtime
Instead of providing the container ID through the environment, you can also pass it through the constructor – which is handy when you're providing the user with a setting through theme options in the database, for example.
$containerId = get_option('namespace_gtm_container_id', null); new \Gebruederheitz\Wordpress\GoogleTagManager($containerId);
An ID passed to the constructor will always override the environment setting.
Using a custom template
By creating a file at template-parts/blocks/gtm.php
inside your theme directory
you can override the default output (replace the snippet).
Alternatively you may pass a custom path (inside your theme directory) as the constructor's second parameter and use that file instead.
new \Gebruederheitz\Wordpress\GoogleTagManager(null, 'partials/tagmanager.twig.php');