Welcome to WebmasterWorld Guest from 34.231.247.139

Forum Moderators: coopster & jatar k

Reading DataLayer values in GTM

     
12:57 pm on Jul 1, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 25, 2006
posts: 1105
votes: 1


I like to store Event value into PHP value. How to do this in the correct way?
“event”: “ipEvent”,
“ipCountry”: “IE”,
“gtm.uniqueEventId”: 6
},
2:14 pm on July 1, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 284


Do you mean like that?

$value=['event'=>'ipEvent','ipCountry'=>'IE','gtm.uniqueEventId'=>6];
8:21 pm on July 2, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 25, 2006
posts: 1105
votes: 1


<script type="application/javascript">
function callback(json) {
dataLayer.push({'event': 'ipEvent', 'ipCountry': json.country});
}
</script>
2:20 pm on July 3, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Nov 13, 2016
posts:1194
votes: 284


Sorry, I have no idea of what you are asking, so I am just popping up your topic, hoping someone will be able to help.
11:13 am on July 4, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 25, 2006
posts: 1105
votes: 1


It is a simple issue: how to get value “ipCountry”: “IE”, from DataLayer? Javascript is also posted.
12:32 pm on July 4, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2692
votes: 815


@toplisek your question is not clear, but what I understand is that your are trying to take values from JS and pass them to PHP.

Note that PHP runs on your server and JS runs on the clients device. These are completely separate. To pass value from JS to PHP you need to send a request to the server with the data using a Fetch request (or AJAX if using jQuery). You will need to set up an endpoint(url) running your PHP script and post the data to that endpoint.

A similar topic was discussed in this thread: [webmasterworld.com...]
12:53 pm on July 4, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 25, 2006
posts: 1105
votes: 1


Yes, will this work when I modify?

<script type = "application/javascript">
function callback(json) {
dataLayer.push({'event': 'ipEvent', 'ipCountry': json.country});
fetch('/myreceivedcountrycode.php?country=' + json.country)
.catch(console.error);
}
</script>
1:27 pm on July 4, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member Top Contributors Of The Month

joined:Apr 1, 2016
posts:2692
votes: 815


Yes in principle this will work.

Just to be clear here is a step through the function:

This assumes that your object 'json', that you are passing to the function already includes the value for json.country.

This line:
dataLayer.push({'event': 'ipEvent', 'ipCountry': json.country}); 

is updating the dataLayer object with key value pairs event: 'ipEvent' and ipCountry with the value of json.country.

The line:
fetch('/myreceivedcountrycode.php?country=' + json.country) 

is making a get request to the url /myreceivedcountrycode.php?country=' + json.country

Note that you are not reading anything from the dataLayer object your are only assign key/value pairs to it.

If this is what you wanted to achieve it should work.

One final caveat, by concatenating the value of json.country to the end of the the url one runs the risk that that value contains characters that are not acceptable for url encoding. Now in your specific case, I assume, the values are two letter country codes so it is likely not an issue, but if it may be an issue you would need to handle that exception/case.
11:07 am on July 5, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Oct 25, 2006
posts: 1105
votes: 1


.catch(console.error); shows an error in validation. How to fix this?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members