Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integration with tag manager #115

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"autoload": {
"files": [
"inc/namespace.php",
"inc/consent/namespace.php",
"inc/gtm/namespace.php"
"inc/consent/namespace.php"
]
},
"extra": {
Expand Down
37 changes: 36 additions & 1 deletion docs/consent/google-tag-manager.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
# Google Tag Manager Consent Integration

Support for Google Tag Manager is provided via GTM's data layer variable. When either the page loads or consent is subsequently changed the data layer is updated allowing you to create triggers based on the consent given.
Support for Google Tag Manager (GTM) can be added via GTM's data layer variable.

You will need to add the following PHP file to your application somewhere and load it. It can be a `mu-plugin`, or part of a theme for example.

```php
<?php
/**
* Altis Consent Tag Manager Integration
*/

namespace Altis\Consent\GTM;

add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_scripts', 11 );

/**
* Add inline tag manager script to Consent API JS.
*/
function enqueue_scripts() {
wp_add_inline_script(
'altis-consent',
sprintf(
// Ensure data layer variable is available.
'var %1$s = window.%1$s || [];' .
// Push initial consented categories into data layer.
'%1$s.push( { event: \'altis-consent-changed\', altisConsent: Altis.Consent.getCategories().join( \', \' ) + \',\' } );' .
// Listen for updates to consented categories.
'document.addEventListener( \'wp_listen_for_consent_change\', function () { %1$s.push( { event: \'altis-consent-changed\', altisConsent: Altis.Consent.getCategories().join( \', \' ) + \',\' } ) } );',
// If using the HM GTM plugin this will ensure the right data layer variable name is used.
apply_filters( 'hm_gtm_data_layer_var', 'dataLayer' )
),
'after'
);
}
```

When either the page loads or consent is subsequently changed the data layer is updated allowing you to create triggers based on the consent given.

The event name is `altis-consent-changed` and the variable is named `altisConsent`. The variable data is a list of the [consent categories](./consent-api.md#consent-categories) currently given for example "functional, statistics-anonymous".

Expand Down
45 changes: 0 additions & 45 deletions inc/gtm/namespace.php

This file was deleted.

1 change: 0 additions & 1 deletion inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
*/
function bootstrap() {
Consent\bootstrap();
GTM\bootstrap();
}