简体   繁体   中英

querying the google tag manager data layer using php

Has anyone ever queried the GTM JS datalayer directly from PHP? So far I can't find anything by searching Google etc.

Here's some context for readers:

  1. I have a Wordpress website and am building a custom navigation system in my child theme function.php file. As part of this ecosystem we have a .NET App that users log into. (Think of the WP site as a landing screen/intro/wrapper).
  2. Google Tag Manager is used across both the Wordpress site and our .NET app. The app populates the data layer with many things such as details about the user ie. username, userID, balance etc. And, users can freely move between the App and the Wordpress site that sits in front of it.
  3. Now, what I'm hoping to do in WordPress (from PHP) is actually query the GTM datalayer so I can grab the logged in user details etc. and present those inside my WordPress nav.

This approach may well be arse-about-face as I could probably also get such data from a Cookie within the domain BUT for interest sake I thought I'd go down this PHP route.

The GTM datalayer is a Javascript variable that exists only in the rendered frontend code. Since PHP runs on the server end of things it does not have access to the datalayer.

Well, technically you could do funny things like creating an AJAX call in the frontend to retrieve the page with the dataLayer and parse it, but that would be a very awkward way of doing things; it would be probably easier to hook into the serverside process that creates the dataLayer code in the first place.

If you want to get creative you could eg build a basic API of sorts that puts the datalayer values in a JSON object and load that from both your WP and .net pages - dataLayer as a service, so to speak. That still seems better than having your WP page scraping the .net app (which seems to be the only alternative).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM