Problem/Motivation

As a DevOps practitioner needing to provision many Drupal sites, I want to be able to programmatically create a Google Tag Manager container so that I can scale my operations.

Proposed resolution

Add a drush command that provides a method for creating a Google Tag Manager container with default settings, the label and GTM ID passed as parameters.

Issue fork google_tag-3356152

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mark_fullmer created an issue. See original summary.

mark_fullmer’s picture

Status: Active » Needs review
StatusFileSize
new2.11 KB
new2.44 KB

Two patches are attached, one for 2.0.x and one for 8.x-1.x.

The approach here is to provide an MVP, where the drush command simply uses the settings defaults and adds a GTM ID.

Subsequent work could be done to provide more parameters in the drush command.

japerry’s picture

Status: Needs review » Needs work

For 2.x at least, all validation is skipped, so this patch should at least ensure the same validation we use in the form applies to the command.

mark_fullmer’s picture

Status: Needs work » Needs review
StatusFileSize
new2.35 KB

this patch should at least ensure the same validation we use in the form applies to the command.

Good call. The attached patch adds validation using the existing pattern from TagContainer::GOOGLE_TAG_MATCH in the Drush command.

mmarler’s picture

Status: Needs review » Reviewed & tested by the community

I was able to successfully create containers with drush. Validation checks out as well. Moving to RTBC.

mark_fullmer’s picture

Issue summary: View changes
mark_fullmer’s picture

Requesting maintainer consideration for merging & inclusion in a release. Thanks!

japerry’s picture

Status: Reviewed & tested by the community » Fixed

Got a chance to take a closer look at this, and MR!139 is pretty good. Had to make a few changes including:

  • Converted the command to a Symfony Console command, which Drush 13.7 and 14 discover automatically. Drush 14 deprecates DrushCommands, annotations and drush.services.yml, so those are removed.
  • The command builds itself with its own create() rather than Drush's AutowireTrait, which Drush 15 removes, so it has no Drush dependency and its test runs without Drush.
  • The tag ID must match the whole pattern and is limited to 20 characters, as on the form. Previously "fooGTM-1234bar" was accepted.
  • Errors now exit non-zero, so provisioning scripts stop on a bad ID.
  • New containers get every event with its default settings and a generated machine name, the same as a container added through the form. Previously the events were left empty, and reusing a tag ID failed with "already exists".
  • The command now fails if a container already uses the tag ID, so running a provisioning script twice doesn't create a second container and make it the default. --force creates another one anyway.
  • Only the machine name is printed, so scripts can capture it.
  • Added DrushCommandsTest, covering creation, the label default, --force, an already used tag ID and invalid IDs.

Committed!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.