0

I have this redirect link, www.example.com/redirect which identifies if the mobile device is iOS or Android, after that it redirects to different links based on the device. I have tried different methods found in this forum but it never worked.

Below is the latest code I've tried:

<script>
$(document).ready(function(){
function getMobileOperatingSystem() {
 var userAgent = navigator.userAgent || navigator.vendor || window.opera;
 // Windows Phone must come first because its UA also contains "Android"
 if (/android/i.test(userAgent)) {
 return "Android";
 }
 // iOS detection from: http://stackoverflow.com/a/9039885/177710
 if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
 return "iOS";
 }
}
 function DetectAndServe(){
if (getMobileOperatingSystem() == "Android") {
 ga('send', 'pageview', {
 'page': '/33red33',
 'hitCallback': function() {
 window.location.href = 'http://android.com';
 }
 }); 
 }
if (getMobileOperatingSystem() == "iOS") {
 ga('send', 'pageview', {
 'page': '/33red33',
 'hitCallback': function() {
 window.location.href = 'http://apple.com';
 }
 }); 
 }
};
});
</script>
halfer
20.2k19 gold badges110 silver badges207 bronze badges
asked Oct 26, 2017 at 8:25

1 Answer 1

1

This may be just a typo on here but nowhere in your code are you actually calling the function DetectAndServe, change your code to the below and it should work.

<script>
$(document).ready(function(){
function getMobileOperatingSystem() {
 var userAgent = navigator.userAgent || navigator.vendor || window.opera;
 // Windows Phone must come first because its UA also contains "Android"
 if (/android/i.test(userAgent)) {
 return "Android";
 }
 // iOS detection from: http://stackoverflow.com/a/9039885/177710
 if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
 return "iOS";
 }
}
 function DetectAndServe(){
if (getMobileOperatingSystem() == "Android") {
 ga('send', 'pageview', {
 'page': '/33red33',
 'hitCallback': function() {
 window.location.href = 'http://android.com';
 }
 }); 
 }
if (getMobileOperatingSystem() == "iOS") {
 ga('send', 'pageview', {
 'page': '/33red33',
 'hitCallback': function() {
 window.location.href = 'http://apple.com';
 }
 }); 
 }
};
DetectAndServe();
});
</script>
answered Oct 29, 2017 at 10:35
Sign up to request clarification or add additional context in comments.

1 Comment

yea i figured it out myself a couple of days ago....cant believe it was something like this, brain lag lol. thanks for your input !

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.