``A fire tag is assigned depending on the existence of a div element

I have a task I need to complete on Wordpress blog pages, however there is no specific identifier in the URL.

Instead, there are differences in content that can be used.

Is there a method to trigger a tag (or event) based on the presence of a particular div? For example, if a certain div only appears on the pages where I want the tag triggered.

Any suggestions?

Answer №1

To track pageviews with a custom variable of the DOM type, follow these steps: First, navigate to variables and create a new one. Select "DOM Element", set the element as "body", and the attribute name as "class". Next, create a new trigger, choose "pageview", set it to "fire on some pages", select your custom variable as the condition, choose matchtype "contains", and input the classname (e.g. "page-321"). Since WordPress uses multiple classes on the body, it's important to use "contains" instead of "equals" for the match type.

If you have inserted the Google Tag Manager code in the header, you will need to change the pageview trigger to fire on "DomReady" (unless the GTM implementation follows the documentation and the code is placed after the body tag).

Answer №2

Indeed, there is a solution available by utilizing custom events triggered within the dataLayer.

If the particular div you are seeking to identify is present on DOM Ready and is labeled 'mydiv', then simply check for its existence and push an event to the dataLayer like so:

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

This code can be placed in various locations, but it may be most effective to incorporate it into GTM as a 'custom HTML' tag that triggers on all pages.

In addition, you can set up a trigger of the 'custom event' type to listen for the event named 'mydiv_exists', enabling further customization for tags to activate upon this trigger.

If necessary, you can also include other data into the dataLayer simultaneously with this event.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Browser fails to display input value when altered

I have noticed that when the value of the Input element changes, the browser does not display the updated value. However, you can verify this change by inspecting the DevTools in the browser. I am curious as to why the actual value of the Input element is ...

Switch out the ajax data in the input field

Is there a way to update the value in a text box using Ajax? Below is my code snippet: <input type="text" id="category_name" name="category_name" value="<?php if(isset($compName)) { echo ucfirst($compName); ...

Is it possible to declare variables within a React 'render' function?

I have data arranged in multiple columns on several rows, with a react element corresponding to each data element. The number of elements on the first row can vary between 4 and 6. For instance, I may display a user's name, birthday, and email. If t ...

Having trouble binding form data to a React component with the onChange() method?

I've been working on developing an email platform exclusively for myself and encountered a roadblock with this React form not updating state when data is entered. After identifying the issue, it appears that the main problem lies in the React form not ...

How to send emails with attachments using WordPress?

Looking for assistance in sending emails with attachments using Wordpress. I am struggling and believe there might be something missing in my code. Any help would be greatly appreciated! :) Below is the code spread across two files: Name (required)<b ...

What is the appropriate way to retrieve an array that has been stored in the this.state property?

https://i.stack.imgur.com/y9huN.jpgAs a newcomer to react, I have been exploring the react documentation on making Ajax calls. While the docs make it seem simple to retrieve JSON information and set it to a state variable, I've encountered some challe ...

Axios displays a status code of 0 instead of the expected 403

Trying to monitor the responses of requests using axios response interceptors has been quite a challenge for me. In one specific request that necessitates authorization, everything goes smoothly when the token is valid, and data is returned without any is ...

Encountered a "Transformer is not a constructor" error when trying to upgrade the React Native SDK version from 0.61.5 to 0.64

https://i.sstatic.net/LYdxj.pngRecently, I upgraded my react native version to the latest one and encountered an error stating "Transformer is not a constructor". The metro-react-native-babel-preset version I am currently using is 0.64.0. Can someone ple ...

Converting Firebase TIMESTAMP values to human-readable date and time

Utilizing Firebase in my chat application, I am adding a timestamp to the chat object using the Firebase.ServerValue.TIMESTAMP method. I want to display the time when the message was received in the chat application using this timestamp. If it is the cur ...

RouterContext Error: Invariant violation: Do not utilize <withRouter(App) /> in a context without a <Router> present

After wrapping my app with BrowserRouter and trying to export it as withRouter(App), I encountered the following error in the browser: 16 | return ( 17 | <RouterContext.Consumer> 18 | {context => { > 19 | invariant( | ^ ...

"Encountering an error while parsing the result of an HTTP request in Node.js

Receiving information from an external api: var requestData = http.get('http:...format=json', function(responseFromApi) { if(responseFromApi.statusCode !== 200){ res.status(400).send(data); return; } responseFromApi.on('data&a ...

AngularJS - ng-repeat: Warning: Repeated items found in the repeater and are not allowed. Repeater:

I'm currently using ng-repeat to showcase a collection of items fetched from the Twitter API. However, I am encountering an issue where Angular attempts to display the empty list while the request is still being processed, resulting in the following e ...

Updating an object with AngularJS

Let's consider the code snippet below: var absenceType = {name: 'hello'}; this.newAbsenceType = angular.copy(absenceType); After making changes to this.newAbsenceType, you want to apply these changes to the original object. I have explore ...

Updating a slider based on the values of two other sliders can be achieved by implementing a

I am working on a project that involves three input sliders. I need the third slider to display the product of the values from the first two sliders. Additionally, I want the value of the third slider to update dynamically whenever the values of the first ...

Can TypeScript be used to dynamically render elements with props?

After extensive research on SO and the wider web, I'm struggling to find a solution. I have devised two components, Link and Button. In short, these act as wrappers for <a> and <button> elements with additional features like chevrons on t ...

Issue with Bootstrap Scrollspy: Scrollspy function not functioning as expected

I need help with creating a one-page website where the navbar links change based on the section of the page you are on. I tried implementing it using HTML, but it didn't work out as expected. The code I used was within the container holding different ...

Is there a way to allow for clicking on a row to redirect to a new tab and display the data of the selected row?

Currently, I am working with mui-datatable and trying to figure out how to enable the user to be directed to another page simply by clicking on a row. Additionally, I need the data of that specific row to be passed along to the new page as well. The error ...

What is the reason for injecting dependencies twice in AngularJS?

I'm a beginner in Angular and I'm curious to understand the reasoning behind injecting all our dependencies twice. Here is an example: var analysisApp=angular.module('analysisApp',[]); analysisApp.controller('analysisController ...

NodeJS and ExpressJS fail to redirect to the main landing page

I am currently developing a shopping cart application using nodejs/expressjs. I have encountered an issue with redirecting back to the homepage ('/') after the user submits their credentials during sign up. Despite my search for relevant articles ...

When trying to load a php page2 into page1 via ajax, the Javascript code fails to execute

Currently, I am in the process of learning PHP and JavaScript. I have encountered a particular issue with a webpage setup. Let's say I have a page called page1 which consists of two input fields and a button labeled 'Go'. Upon clicking the & ...