• Resolved davidsimon14

    (@davidsimon14)


    Hey guys!

    I am looking to find a solution to execute a datalayer.push script (either inside php or directly in js) when a file is opened.

    Example: this page (https://proximite-courtage.fr/actualites/proximite-rendement-durable/) contains a link that directs to (https://www.proximite-courtage.fr/pdf/DICI_PROXIMIT%C3%89%20RENDEMENT%20DURABLE_FR0014007BD1.pdf).

    I was under the impression that there is no hook or filter for that, but if there is, please I will be happy to be wrong.

    If not, how the hell am I supposed to implement that?

    I could just create a class and call the datalayer onClick, but it means I need to attribute the class to each file tag, which I would like to avoid because it’s my client that creates the content, and i’d rather not tell him to attribute this class to each file that needs tracking.

    Is there a better way to do this?

    Thanks!

    PS: I have been a dev for 15 years, but pretty new to web and PHP/Wordpress, so I might be pretty ignorant with some stuff.

    • This topic was modified 1 year, 8 months ago by davidsimon14.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    PHP would be of no help in calling datalayer.push() since PHP executes server side and we’re talking about client side scripting. You need to call it from an onclick listener. The issue is listen for a click on what? If it’s something a naive client created, how to find the right elements to listen to?

    Your script could search for anchor tags in the DOM which contain .pdf href attributes and listen on any that are found. I’m not sure where you’re getting the data that is to be sent though. That might be where PHP could come in, since it has access to the entire DB. Using “the_content” filter, PHP could search content for .pdf links and modify them with appropriate anchor tag attributes whose values you’d want to send to Google.

    Thread Starter davidsimon14

    (@davidsimon14)

    Thank you for your answer.

    It might be a solution: when the page is loaded, I search through the_content() for a pdf file and if I find one, I can give it the class and data I want to trigger the script onClick. Thanks, I might try that, in the meantime, still looking for a lighter solution.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘GTM – Datalayer.push on file opening’ is closed to new replies.