/** * Provide routing for deep links. */ let isMobile = null; let racetracWebURL = webPageConfiguration.RACETRAC_WEB_URL; const knownWebRedirections = ["coupons","promocodes","upgraderewardcard"]; const webRedirectionFeatureURLS = { "coupons": webPageConfiguration.COUPONS_WEB_REDIRECT_FEATURE_URL, "promocodes": webPageConfiguration.PROMOCODES_WEB_REDIRECT_FEATURE_URL, "upgraderewardcard": webPageConfiguration.UPGRADE_REWARD_CARD_FEATURE_URL } $(document).ready(function () { isMobile = initializeGlobalVariables(); redirectToDeeplink(); // Do it! }); function initializeGlobalVariables() { // after document.ready return { Android: function () { return navigator.userAgent.match(/Android/i); }, iOS: function () { return navigator.userAgent.match(/iPhone|iPad|iPod/i); } }; } function redirectToDeeplink() { /* Checking if the environment is Preprod(QA) or Production. Based on that, deciding whether to open the QA version of app(if installed), or to redirect to preprod/production RT website. */ var suffix = location.host.indexOf("dep-api") >-1 ? webPageConfiguration.SUFFIX : ""; if (suffix) { suffix += ";S.browser_fallback_url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3D"+webPageConfiguration.RACETRAC_PACKAGE; } try { // initializing deeplink feature URl with default value. let featureURL = "?featuretype=''"; // Getting query params from the url; const queryParamsString = location.search.substr(1); if (queryParamsString) { // if any query params, overriding default value; featureURL = ''; if (isMobile.Android() || isMobile.iOS()) featureURL = buildAppDeeplinkURL(queryParamsString); else racetracWebURL += buildWebRedirectionURL(queryParamsString); } // Redirect as follows: // .. to targeted deep link if on device and app is installed, or // .. to store if on device and app is not installed, or // .. to RaceTrac.com site as fallback if (isMobile.Android()) { androidRedirection(featureURL, suffix); } else if (isMobile.iOS()) { iosRedirection(featureURL); } else { webRedirection(racetracWebURL); } } catch (err) { window.location.replace(racetracWebURL); } } function buildAppDeeplinkURL(queryParamsString) { let deeplinkURL = ''; queryParamsString.split("&").forEach(function (singleQueryParam, index) { let keyValuePair = singleQueryParam.split("="); let key = keyValuePair[0]; let value = keyValuePair[1]; let seperator = index ? "&" : "?"; // Previous production deeplinks use linktarget to denote feature type. if (key == "linktarget") { key = "featuretype"; } deeplinkURL += seperator+key+"="+decodeURIComponent(value); }); return deeplinkURL; } // Input URL : deeplinks/deeplinkRedirect.html?linktarget=coupons&id=E_ZZZ_0001 // Output URL : https://racetrac.com/Rewards/Coupons?id=E_ZZZ_0003 function buildWebRedirectionURL(queryParamsString) { let featureRedirectionPath = ''; let featureQueryParams = '' let index = 0; queryParamsString.split("&").forEach(function (singleQueryParam) { let keyValuePair = singleQueryParam.split("="); let key = keyValuePair[0]; let value = keyValuePair[1]; //key = linktarget, value = coupons if (key == "linktarget"){ if(knownWebRedirections.indexOf(value) > -1) { featureRedirectionPath = webRedirectionFeatureURLS[value]; } } else { let seperator = index ? "&" : "?"; featureQueryParams += seperator+key+"="+encodeURIComponent(decodeURIComponent(value)); // increment index to change the seperator from ? to & index++; } }); return featureRedirectionPath+featureQueryParams; } function webRedirection(racetracWebURL) { window.location.replace(racetracWebURL); } function androidRedirection(featureURL, suffix) { const url = webPageConfiguration.ANDROID_REDIRECT_PREFIX+featureURL+"#Intent;scheme=racetrac;package="+webPageConfiguration.RACETRAC_PACKAGE+suffix+";end"; window.location.replace(url); } /* This will handle the redirection for the IOS. */ function iosRedirection(featureURL) { $(".notification-msg").show(); window.location.replace(webPageConfiguration.IOS_REDIRECT_PREFIX + featureURL); }