javascript - Fire tag based on a div existing - Stack Overflow

So I've got something that needs to fire on Wordpress blog pages, but there is nothing in the URL

So I've got something that needs to fire on Wordpress blog pages, but there is nothing in the URL to help identify them.

What does exist are content differences.

Is there a way to fire a tag (or an event first and then fire off that event), based on the existence of a specific div? for example only exists on the pages I want to fire the tag on.

Any ideas?

So I've got something that needs to fire on Wordpress blog pages, but there is nothing in the URL to help identify them.

What does exist are content differences.

Is there a way to fire a tag (or an event first and then fire off that event), based on the existence of a specific div? for example only exists on the pages I want to fire the tag on.

Any ideas?

Share Improve this question asked Nov 3, 2015 at 22:59 TopherTopher 1831 gold badge2 silver badges13 bronze badges 2
  • Worpress put id in the body element such as page-277, just filter pages with this like #page-277 div.mydiv – Carlos2W Commented Nov 3, 2015 at 23:01
  • So it does have .page-id-319 but I'm still not sure how to fire a tag in gtm off the existence of .page-id. Any thoughts? – Topher Commented Nov 3, 2015 at 23:15
Add a ment  | 

2 Answers 2

Reset to default 9

Use a pageview trigger with a custom variable of the DOM type. I.E. go to variables, new, select "DOM Element", set element as "body" and attribute name as "class". Then create a new trigger, select "pageview", set to "fire on some pages", select as condition your custom variable, matchtype "contains" and the classname (i.e "page-321") - WP uses multiple classes on the body so you have to use "contains" instead of "equals" as a match type.

If you have put the GTM code in the header you have to switch the pageview trigger to fire on "DomReady" (if you have implemented GTM per documentation the GTM code is after the body tag so the default will work).

yes, there is a way, you need to use custom events fired into the dataLayer.

I assume the div you want to detect is available on DOM Ready and is called 'mydiv'. Just detect whether 'mydiv' exists, if it does push an event to the dataLayer as such:

if(document.getElementById('mydiv')){
  dataLayer.push({'event':'mydiv_exists'});
}

You can place this code in many different locations but your best bet might be to put it in GTM as a 'custom HTML' tag which fires on all pages.

You can then create a trigger of type 'custom event' which looks for the event name 'mydiv_exists' and you can configure further tags to fire on this trigger.

You can also push other data into the dataLayer at the same time as this event if required.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744136131a4560065.html

相关推荐

  • javascript - Fire tag based on a div existing - Stack Overflow

    So I've got something that needs to fire on Wordpress blog pages, but there is nothing in the URL

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信