Forum Moderators: coopster

Message Too Old, No Replies

Reading DataLayer values in GTM

         

toplisek

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

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



I like to store Event value into PHP value. How to do this in the correct way?
“event”: “ipEvent”,
“ipCountry”: “IE”,
“gtm.uniqueEventId”: 6
},

Dimitri

2:14 pm on Jul 1, 2019 (gmt 0)

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



Do you mean like that?

$value=['event'=>'ipEvent','ipCountry'=>'IE','gtm.uniqueEventId'=>6];

toplisek

8:21 pm on Jul 2, 2019 (gmt 0)

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



<script type="application/javascript">
function callback(json) {
dataLayer.push({'event': 'ipEvent', 'ipCountry': json.country});
}
</script>

Dimitri

2:20 pm on Jul 3, 2019 (gmt 0)

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



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.

toplisek

11:13 am on Jul 4, 2019 (gmt 0)

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



It is a simple issue: how to get value “ipCountry”: “IE”, from DataLayer? Javascript is also posted.

NickMNS

12:32 pm on Jul 4, 2019 (gmt 0)

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



@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...]

toplisek

12:53 pm on Jul 4, 2019 (gmt 0)

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



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>

NickMNS

1:27 pm on Jul 4, 2019 (gmt 0)

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



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.

toplisek

11:07 am on Jul 5, 2019 (gmt 0)

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



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