I need to pass deviceType and userState(IL, IN, IA, KS, etc) variables to GTM for marketing purporses and wondering what’s the best way to do it.

To calculate deviceType I will use mobiledetect php library. For user userState - GeoIP2 php library.

Since these variables are constant per session and in fact per device/user, I am looking at the ways to make it most performant.

I have following ideas:

1] Use Mage::getSingleton to call mobiledetect/GeoIP2 functions

This way it load function once per page load (http request)

2] To further improve 1] since deviceType and userState are same per session I am thinking to write them in session and read from it.

Like this

Mage::getSingleton('core/session')->setDeviceType ($deviceType); -write Mage::getSingleton('core/session')->setDeviceType(); - read when needed it

I assume reading from session will be more performant than recalculating on every page load/http request.

3] To further improve 2] since deviceType and userState variables are not needed for above the fold (not crucial contant), I am thinking about loading functions on document ready using ajax. This way first time they are calculated will not effect page speed. Something like this:

$(window).load(function () {
        url: "deviceType_State.php",

Here we can also add check if the session variable already exists than do not do ajax call.

4] If 3] is good idea, to further improve it instead of writing variables into session on our server side, we could write it into session storage on client’s browser or maybe even better local . sessionStorage.setItem(device', $deviceType); or localStorage.setItem(device', $deviceType); When checking some of big sites i saw they keep similar marketing variables in localStorage.

Please tell if my thinking is right, and what's the best way to achieve the goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.