英文:
Google tag manager matches CSS selector one fires another doesn't they don't fire consistently
问题
在Google标签管理器中,跟踪这个标记:
<div class="swiper-button-prev-recom" tabindex="0" role="button" aria-label="Previous slide" aria-controls="swiper-wrapper-29c7f9f933787763" aria-disabled="false">
<svg width="8" height="13" viewBox="0 0 8 13" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0.261501 5.64373C-0.087168 5.9924 -0.087168 6.57352 0.261501 6.92218L5.53027 12.1909C5.91767 12.5784 6.49879 12.5784 6.84746 12.1909L7.7385 11.3386C8.08717 10.9512 8.08717 10.3701 7.7385 10.0215L3.98063 6.26359L7.7385 2.54446C8.08717 2.19579 8.08717 1.61468 7.7385 1.22727L6.84746 0.374968C6.49879 -0.0124407 5.91768 -0.0124408 5.53027 0.374968L0.261501 5.64373Z" fill="#757575"></path>
</svg>
</div>
使用触发器:
"Click Element Matches CSS Selector .swiper-button-prev-recom svg"
运行正常。
但是尝试跟踪以下标记时:
<div class="swiper-button-next-recom" tabindex="0" role="button" aria-label="Next slide" aria-controls="swiper-wrapper-29c7f9f933787763" aria-disabled="false">
<svg width="8" height="13" viewBox="0 0 8 13" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.7385 6.92218C8.08717 6.57352 8.08717 5.9924 7.7385 5.64373L2.46973 0.374968C2.08232 -0.0124408 1.50121 -0.0124407 1.15254 0.374968L0.261501 1.22727C-0.0871676 1.61468 -0.0871675 2.19579 0.261501 2.54446L4.01937 6.30233L0.261501 10.0215C-0.0871672 10.3701 -0.0871671 10.9512 0.261501 11.3386L1.15254 12.1909C1.50121 12.5784 2.08232 12.5784 2.46973 12.1909L7.7385 6.92218Z" fill="#757575"></path>
</svg>
</div>
使用触发器:
"Click Element Matches CSS selector .swiper-button-next-recom svg"
却无法正常工作。为什么?
我问了ChatGPT,以下是它的回答:
触发器 "Click Element Matches CSS selector .swiper-button-next-recom svg" 应该可以用于跟踪点击下一张箭头,前提是 CSS 选择器或触发器配置没有问题。
以下是可能导致不起作用的几个原因:
选择器特异性:确保 CSS 选择器 .swiper-button-next-recom svg 准确地匹配标记中的下一张箭头元素。仔细检查类名,确保没有拼写错误或额外的空格。还有可能存在与元素相关的冲突的 CSS 样式或JavaScript修改,从而阻止触发器触发。您可以使用浏览器的开发工具检查元素,以验证选择器是否正确地定位了下一张箭头。
触发器顺序:验证下一张箭头的触发器是否在Google标签管理器中正确地定位。如果有多个触发器匹配相同的点击事件,触发器的顺序很重要。确保下一张箭头的触发器位于可能干扰其触发的任何其他触发器之前。
事件传播:如果与下一张箭头元素相关的事件监听器或JavaScript代码阻止了点击事件传播到文档,触发器可能不会触发。如果存在自定义事件处理程序或拦截点击事件的JavaScript框架,可能需要使用自定义JavaScript代码来手动跟踪点击事件。
为了解决此问题,您可以执行以下操作:
验证CSS选择器:使用浏览器的开发工具检查下一张箭头元素,并确认CSS选择器 .swiper-button-next-recom svg 是否准确地定位它。
测试触发器:创建一个带有不同条件的测试触发器,例如 "Click Element - matches CSS selector - .swiper-button-next-recom",以查看是否正确捕获点击事件。如果它可以工作,可能是您原始触发器中使用的CSS选择器存在问题。
检查JavaScript冲突:暂时删除可能干扰点击事件的自定义JavaScript代码或第三方脚本。然后,测试触发器是否正确触发。如果它可以工作,逐渐重新引入JavaScript代码以确定是否存在冲突。
通过查看CSS选择器、触发器顺序和潜在的JavaScript冲突,您应该能够解决此问题并成功跟踪点击下一张箭头。
执行以下操作:
document.querySelectorAll('.swiper-button-prev-recom svg')
会给出正确的元素。
我不觉得
英文:
In google tag manager, tracking this markup
<div class="swiper-button-prev-recom" tabindex="0" role="button" aria-label="Previous slide" aria-controls="swiper-wrapper-29c7f9f933787763" aria-disabled="false">
<svg width="8" height="13" viewBox="0 0 8 13" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0.261501 5.64373C-0.087168 5.9924 -0.087168 6.57352 0.261501 6.92218L5.53027 12.1909C5.91767 12.5784 6.49879 12.5784 6.84746 12.1909L7.7385 11.3386C8.08717 10.9512 8.08717 10.3701 7.7385 10.0215L3.98063 6.26359L7.7385 2.54446C8.08717 2.19579 8.08717 1.61468 7.7385 1.22727L6.84746 0.374968C6.49879 -0.0124407 5.91768 -0.0124408 5.53027 0.374968L0.261501 5.64373Z" fill="#757575"></path>
</svg>
</div>
With the trigger:
"Click Element Matches CSS Selector .swiper-button-prev-recom svg"
Works fine.
But trying to track:
<div class="swiper-button-next-recom" tabindex="0" role="button" aria-label="Next slide" aria-controls="swiper-wrapper-29c7f9f933787763" aria-disabled="false">
<svg width="8" height="13" viewBox="0 0 8 13" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.7385 6.92218C8.08717 6.57352 8.08717 5.9924 7.7385 5.64373L2.46973 0.374968C2.08232 -0.0124408 1.50121 -0.0124407 1.15254 0.374968L0.261501 1.22727C-0.0871676 1.61468 -0.0871675 2.19579 0.261501 2.54446L4.01937 6.30233L0.261501 10.0215C-0.0871672 10.3701 -0.0871671 10.9512 0.261501 11.3386L1.15254 12.1909C1.50121 12.5784 2.08232 12.5784 2.46973 12.1909L7.7385 6.92218Z" fill="#757575"></path>
</svg>
</div>
With:
"Click Element Matches CSS selector .swiper-button-next-recom svg"
Does not work. Why not?
I asked chatGPT and this is what it said:
> The trigger "Click Element Matches CSS selector
> .swiper-button-next-recom svg" should work to track clicks on the next
> arrow, assuming there are no issues with the CSS selector or the
> configuration of the trigger.
>
> Here are a few possible reasons why it might not be working:
>
> Selector specificity: Ensure that the CSS selector
> .swiper-button-next-recom svg accurately matches the next arrow
> element in the markup. Double-check the class name and make sure there
> are no typos or extra spaces. It's also possible that there are
> conflicting CSS styles or JavaScript modifications affecting the
> element, preventing the trigger from firing. You can inspect the
> element in the browser's developer tools to verify that the selector
> correctly targets the next arrow.
>
> Trigger order: Verify that the trigger for the next arrow is
> positioned correctly in Google Tag Manager. If you have multiple
> triggers that match the same click event, the order of the triggers
> matters. Make sure the trigger for the next arrow is placed before any
> other triggers that might interfere with its firing.
>
> Event propagation: If there are event listeners or JavaScript code
> attached to the next arrow element that prevents the click event from
> propagating to the document, the trigger may not fire. This can happen
> if there are custom event handlers or JavaScript frameworks
> intercepting the click event. In such cases, you might need to use
> custom JavaScript code to manually track the click event.
>
> To troubleshoot the issue, you can:
>
> Verify the CSS selector: Use the browser's developer tools to inspect
> the next arrow element and confirm that the CSS selector
> .swiper-button-next-recom svg accurately targets it.
>
> Test the trigger: Create a test trigger with a different condition,
> such as "Click Element - matches CSS selector -
> .swiper-button-next-recom", to see if it correctly captures the click
> event. If it does, there might be an issue with the CSS selector used
> in your original trigger.
>
> Check for JavaScript conflicts: Temporarily remove any custom
> JavaScript code or third-party scripts that may interfere with the
> click event. Then, test if the trigger fires correctly. If it does,
> gradually reintroduce the JavaScript code to identify any conflicts.
>
> By reviewing the CSS selector, trigger order, and potential JavaScript
> conflicts, you should be able to troubleshoot the issue and track
> clicks on the next arrow successfully.
Doing
document.querySelectorAll('.swiper-button-prev-recom svg')
gives me the right element.
The information in that answer I don't find helpful.
How do you get the arrow to fire consistently?
答案1
得分: 1
"Click Element" 指的是精确选择的元素。如果您点击子元素,例如链接内部的内联 span
,将处理子元素而不是链接。
将触发器从 "Click Element" 更改为 "Click Class" 可以防止这种行为。
示例 Click Element,CSS 选择器 div > svg
:
点击 SVG 并意外选择 div > svg > path
。现在 path
是您的 "Click Element",而 div > svg
的触发器不会触发。
示例 Click Class,CSS 选择器 div > svg
:
点击 SVG 并意外选择 div > svg > path
。尽管如此,svg
会作为 "Click Class" 处理,因为 path
是选择器范围的子元素。
英文:
Click Element
refers precisely to the selected element. If you click on a child element, f.e. an inline span
inside a link, it will be handled instead of the link.
Changing your trigger from Click Element
to Click Class
should prevent this behaviour.
Example Click Element, CSS Selector div > svg
:
Click on the SVG and accidentally select div > svg > path
. Now path
is your Click Element
and the trigger for div > svg
will not fire.
Example Click Class, CSS Selector div > svg
:
Click on the SVG and accidentally select div > svg > path
. Nevertheless, the svg
is handled as Click Class
because path
is a child of the selector scope.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论