Created
February 14, 2018 09:36
-
-
Save ShuvoHabib/72170314fe7c470fbfd6f5f3e444254a to your computer and use it in GitHub Desktop.
Optimizely Device Detector Segment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var deviceDetector = (function () { | |
var ua = navigator.userAgent.toLowerCase(); | |
var detect = (function(s) { | |
if(s===undefined){ | |
s=ua; | |
} else { | |
ua = s.toLowerCase(); | |
} | |
if(/(ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP))))/.test(ua)){ | |
return 'tablet'; | |
} else{ | |
if(/(mobi|ipod|phone|blackberry|opera mini|fennec|minimo|symbian|psp|nintendo ds|archos|skyfire|puffin|blazer|bolt|gobrowser|iris|maemo|semc|teashark|uzard)/.test(ua)){ | |
return 'phone'; | |
} else { | |
return 'desktop'; | |
} | |
} | |
}); | |
return{ | |
device:detect(), | |
detect:detect, | |
isMobile:((detect()!='desktop')?true:false), | |
userAgent:ua | |
}; | |
}()); | |
var deviceDetected; | |
if(deviceDetector.device == 'desktop'){ | |
deviceDetected = 'desktop'; | |
} else { | |
if(deviceDetector.device == 'tablet'){ | |
deviceDetected = 'tablet'; | |
} else{ | |
deviceDetected = 'mobile'; | |
} | |
} | |
//console.log(deviceDetected) | |
window.optimizely = window.optimizely || []; | |
window["optimizely"].push({ | |
type: "user", | |
attributes: { | |
device_type: deviceDetected | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment