Redirect to mobile script on google tag manager

464 views Asked by At

I would like to redirect my website to a mobile version. I have a javascript to do that:

if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
    window.location.href = "http://m.mysite.com";
}

Could I put this code on Google Tag Manager? Is that a good ideia?

Thanks.

1

There are 1 answers

0
Eike Pierstorff On BEST ANSWER

You can put this into GTM using a custom HTML tag that is triggered at page load (you would need to include script tags).

However in my opinion this is not a good idea. GTM is implemented after the opening body tag, which means that before any tags are executed you site loads all assets referenced in the head section (js, css etc.) plus it has to fetch the tag manager script from Google. This introduces a noticable delay before redirecting.

However you could use your script to display a little dialog that asks users if they want to be redirected. If nothing else the dialog would hide the delay (plus some users might prefer the desktop site for whatever reason).