Sponsored Content
The Wayback Machine - https://web.archive.org/web/20210817024838/https://github.com/google/site-kit-wp/issues/433
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

Implement existing snippet detection for Tag Manager #433

Closed
felixarntz opened this issue Aug 22, 2019 · 19 comments
Closed

Implement existing snippet detection for Tag Manager #433

felixarntz opened this issue Aug 22, 2019 · 19 comments

Comments

@felixarntz
Copy link
Member

@felixarntz felixarntz commented Aug 22, 2019 •

Feature Description

We currently detect existing snippets for Analytics and AdSense. This detection should happen for Tag Manager as well, and cover non-AMP as well as AMP.

If an existing tag is found, it should work like in Analytics: If the user has access to that very container, it should be forced. If not, the user should be blocked from proceeding. In both cases, the user should be informed about the existing tag and that it comes from another code source.

In other words, this issue will also include a couple of UI changes/requirements.

This was originally reported as part of #413.

This issue is blocked by #83 and #432, but theoretically implementation can at least start before these are fully completed.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • When the user clicks "connect service" for GTM, Site Kit should confirm whether there's an existing GTM snippet in the FE (placed directly by the user or via another plugin).
  • Assuming is an existing snippet, check if the user's Google account has access to that GTM container (as an Admin https://support.google.com/tagmanager/answer/6107011?hl=en#account_permissions)
  • Display two radio buttons in GTM settings for whether the snippet should be placed or not (useSnippet); in GTM setup this should not be displayed and instead automatically determined:
    • if there is an existing snippet on the page, auto-set this to false
    • if there is no existing snippet on the page, auto-set this to true

Case 1: the account has access to the container:

  • Force-select these and give user option to proceed.
  • Messaging in the UI should be similar to GA ("An existing tag was found on your site with the id XYZ. If later on you decide to replace this tag, Site Kit can place the new tag for you. Make sure you remove the old tag first.")

Case 2: the account doesn't have access to the container:

  • block the user from proceeding (give "back" option)
  • display a message that the user either has to get access as Admin for the container, or remove the old container in order to place a new one: "We've detected there's already an existing Tag Manager tag on your site (ID %s), but your account doesn't seem to have access to this Tag Manager container. You can either remove the existing tag and connect to a different account, or request access to this container from your team."

Implementation Brief

  • Update getExistingTag to support Tag Manager snippet pattern matching for GTM-* container public ID
    • Snippets may be able to be included in different ways so be sure to check for different types of JS code that include GTM. It's worth checking for the official code as well as GTM code supplied by different, popular WordPress plugins such as Monster Insights that enable this functionality. Checking for the AMP and non-AMP versions should also be done.
    • Suppress Tag_Manager tag output for $_GET[tagverify] requests (see Analytics and AdSense modules)
  • Update Tag Manager setup to use getExistingTag
    • If detected, the found container ID is matched against available container IDs that the current user has access to
      The user must have publish permission for the individual container
      • If found and the user has the required permission, pre-select this container and disable the dropdown
        See messaging in AC
      • If found and the does not have the required permission, display error message (see AC) and display "Back" button for user to abort setup.
    • If not detected, attempt to pre-select a container by matching the container name using the same name generated by Tag_Manager::create_container() (#361)
    • If not detected and no matching container, list accounts and containers with none selected
    • Set useSnippet setting based on presence of existing tag (if existing tag, useSnippet = false, otherwise default to true)
  • Update Tag Manager settings component to expose the new useSnippet setting toggle (most of this can be borrowed from Analytics)
  • Add filter to Tag Manager settings option which defaults useSnippet to true if the key does not exist yet

Changelog entry

  • Detect already existing Tag Manager snippets from other sources and inform about them in the setup flow.
@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Sep 17, 2019

@tofumatt

If multiple snippets are found, it would be worth notifying the user that multiple snippets are already in the page and that only one should be used. (There should only be one GTM snippet on the page, correct?)

This is out of scope for this issue, we also don't consider multiple in the other modules. Worth discussing for the future though as a general enhancement.

Similar to how Google Analytics snippets are detected

You don't necessarily need to add this in detail as part of the IB, but just to flag it, it needs careful investigation in which ways Tag Manager can be included in a page, including AMP and non-AMP versions. The regexes used need to provide sufficient coverage of different alternatives. At least a note on this might be good to have here.

@felixarntz felixarntz assigned tofumatt and unassigned felixarntz Sep 17, 2019
@tofumatt
Copy link
Collaborator

@tofumatt tofumatt commented Sep 18, 2019

I've updated the IB to remove the multiple properties part and added the note about being diligent about checking for different Tag Manager include snippets in our checks.

@tofumatt tofumatt assigned felixarntz and unassigned tofumatt Sep 20, 2019
@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Sep 26, 2019

IB ✅

@felixarntz felixarntz removed their assignment Sep 26, 2019
@aaemnnosttv aaemnnosttv self-assigned this Oct 13, 2019
@lilybonney lilybonney added this to the 1.0.0-beta.1.0.9 milestone Oct 14, 2019
@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Oct 15, 2019

Assuming is an existing snippet, check if the user's Google account has access to that GTM container (as an Admin https://support.google.com/tagmanager/answer/6107011?hl=en#account_permissions)

"User" or "Admin" permissions are at the account level. Containers can have individual permissions apart from that.

Do we want to specifically check that the user has Admin level permissions on the container's account or should we instead check for "full" permissions on the container itself (i.e "publish")?

Note: we can check this using the user_permissions API endpoint.
https://developers.google.com/tag-manager/api/v2/reference/accounts/user_permissions/list?authuser=1

@marrrmarrr
Copy link
Collaborator

@marrrmarrr marrrmarrr commented Oct 15, 2019

@aaemnnosttv agreed - better to be more granular here and focus on individual container permissions rather than account-level permissions.

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Oct 15, 2019

Thanks @marrrmarrr this is actually fairly straightforward as Admins inherit the publish permission on each container so we only need to check for that; no need to do any additional checks as to whether it is a direct or inherited permission.

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Oct 16, 2019

@felixarntz @marrrmarrr it seems that much of the acceptance criteria has been borrowed from Analytics but I think there are some important differences to consider here which may not have been evident before.

Perhaps the most fundamental difference is that Analytics also brings data into WordPress, but Tag Manager is currently a simple client-side only integration (outputting the snippet) with no data in WordPress other than account and container names to choose from.

In the case of an existing tag, I don't think we necessarily need to block the user from completing setup or check their permissions. Compared to Analytics, all we're doing is outputting the script on the front end which loads tags from the container. I don't think there is any reason why we can't output more than one (not the case of course with Analytics).

Analytics has the concept (and setting for) whether or not to output the snippet because data can still be shown in the plugin while something else is responsible for outputting the tag which generates that data. This is not the case with Tag Manager as its sole responsibility is outputting a script to load the container. In this case, it doesn't make sense to force the setup to use the account and container IDs from an existing tag because it would simply result in duplicate tags on the front.

For these reasons I think that permissions/access/messaging around existing tags could be greatly simplified/removed with more of a focus on making the user aware, and only block them (perhaps) from setting the site up with the same tag as an existing tag to prevent duplicates.

@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Oct 16, 2019

@aaemnnosttv Good point. I agree we need to ensure there are no duplicate tags in the frontend. However, similar to Analytics we also need to ensure there are not two Tag Manager tags in the frontend, that's why we need this check for existing tag.

In Analytics it's actually the same, it doesn't technically hurt to have more than one tracking snippet, it would just track the same thing to different Analytics properties, and only the data from the one active in Site Kit would show in the plugin. However, we were advised by the Analytics team that this should be avoided.

Summarizing, I think we should for now block users without access the same way we do for Analytics to prevent multiple Tag Manager tags in the frontend. In the case of an existing tag, we then need to set a flag in the database that ensures our own snippet is not printed. I'd suggest just using another useSnippet setting (specific to Tag Manager), which however is not exposed in the UI at all, just auto-populated based on whether there's an existing tag (--> false) or not (--> true).

@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Oct 16, 2019

I think we should for now block users without access the same way we do for Analytics to prevent multiple Tag Manager tags in the frontend.

I'm not entirely sure what you mean here. What does the user's access have to do with preventing multiple tags? If we want to prevent multiple then completing setup shouldn't be possible if there is an existing tag.

In the case of an existing tag, we then need to set a flag in the database that ensures our own snippet is not printed.

Why allow the user to complete the setup if it won't output the tag? This sounds like a confusing experience for the user. If we were to set useSnippet at setup time to false and not output the tag, what would cause it to change to true if there is no UI?

I think we should simply block setup from completing if there is any existing tag if we don't want to allow for multiple.

@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Oct 16, 2019

If we want to prevent multiple then completing setup shouldn't be possible if there is an existing tag.

Yes, but we'd only want to block if the user doesn't have access. If the user has access, we can consider Tag Manager active, force-set the account that has access to the existing tag, and just not print it ourselves (hence the useSnippet: false). That is similar to how we do it in Analytics.

Why allow the user to complete the setup if it won't output the tag? This sounds like a confusing experience for the user.

It's the same for Analytics. We allow users to complete the setup if there is an existing tag they have access to. Under those circumstances, we force-choose that property and don't place the tag ourselves. I think for GTM we can do the same. When you access the module settings, we should re-check for the existing tag, and if there's none anymore, we can silently set useSnippet to true and then also allow the user to change the container.

@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Dec 4, 2019

@aaemnnosttv
@marrrmarrr and I revisited this, let's do the implementation the same way we do for Analytics:

  • If there is an existing snippet and the user does not have access, block them.
  • If they do have access to them, force-select it and do not place our own.

The reasoning is the following: We'll expand what the GTM module does, and even if it technically does nothing at this point, we should allow the user to complete the setup.

Note that we'll also want a setting for whether the snippet is placed, that is initially auto-determined, but the user can adjust it in settings afterwards (again similar to Analytics). I've updated the ACs, sending this back to IB now so that this part can be added.

@felixarntz
Copy link
Member Author

@felixarntz felixarntz commented Dec 19, 2019

IB ✅

@felixarntz felixarntz removed their assignment Dec 19, 2019
@aaemnnosttv aaemnnosttv self-assigned this Jan 2, 2020
@aaemnnosttv aaemnnosttv assigned felixarntz and unassigned aaemnnosttv Jan 8, 2020
@felixarntz felixarntz assigned aaemnnosttv and unassigned felixarntz Jan 8, 2020
@felixarntz felixarntz added this to the 1.1.5 milestone Jan 8, 2020
@aaemnnosttv aaemnnosttv assigned felixarntz and unassigned aaemnnosttv Jan 8, 2020
@felixarntz felixarntz removed their assignment Jan 9, 2020
@cole10up cole10up self-assigned this Jan 9, 2020
@cole10up
Copy link
Collaborator

@cole10up cole10up commented Jan 15, 2020

Tested
Pasted GTM snippet in header and saved
image

Verified snippet detection occurred:
image

Confirmed cancel button functions.

Verified permissions message displays properly when user doesn't have the correct permissions:
Removed permissions
image

image

Picky but @tofumatt, should the above 'Cancel' button be labeled 'Back'?

Passed QA ✅

@cole10up cole10up removed their assignment Jan 15, 2020
@aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented Jan 15, 2020

Picky but @tofumatt, should the above 'Cancel' button be labeled 'Back'?

This is consistent with the other setup modules. Also I think it may not be possible to have different wording for the cancel button currently due the way the setup module components are composed.

@tofumatt
Copy link
Collaborator

@tofumatt tofumatt commented Jan 15, 2020

It's definitely not nice UX though, agreed. We should fix those en-masse—could you create an issue for any modules that should have that wording changed @cole10up?

@marrrmarrr
Copy link
Collaborator

@marrrmarrr marrrmarrr commented Jan 15, 2020

LGTM.
Also SGTM on the s/cancel/back/g for cases where the user hasn't really done any action to cancel, like the one Cole described above.

@cole10up
Copy link
Collaborator

@cole10up cole10up commented Jan 15, 2020

@tofumatt @marrrmarrr I've created this ticket to adjust verbiage where user's presented an expected error message: #1045

I'm unsure of other cases, please advise.

@ThierryA ThierryA removed this from the 1.2.0 milestone Feb 3, 2020
@ThierryA ThierryA added this to the Sprint 14 milestone Feb 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants