var paramsAll; var force; var frontUrl; var redirect; /** * Checks if the current user agent is a mobile device. * @returns {boolean} True if the device is mobile, false otherwise. */ function isMobileDevice() { const userAgent = navigator.userAgent || navigator.vendor || window.opera; return /android|iPhone|iPad|iPod|windows phone|mobile/i.test(userAgent); } /** * Updates the URL based on query parameters, adjusting for mobile devices. * @returns {string} The newly constructed URL. */ function buildNewUrlFromQueryParams() { const url = window.location.href; // Check if there are no query parameters if (!url.includes('?')) { return url; // No query parameters, return the original URL } // Split the URL to get the base URL and query parameters const [baseUrl, queryParamsString] = url.split('?'); frontUrl = baseUrl; // Parse the query parameters const queryParams = new URLSearchParams(queryParamsString); const allUTM = []; let pathSegments = []; const otherParams = []; queryParams.forEach((value, key) => { const decodedValue = decodeURIComponent(value); // Handle special cases like force and mobile-specific URL adjustment if (key === 'param1' && isMobileDevice()) { pathSegments.push(adjustUrlForMobile(decodedValue)); } else if (key === 'f') { force = decodedValue; } else if (key === 'redirect') { redirect = decodedValue === 'true'; // Set redirect to true or false } else if (key.includes('utm')) { allUTM.push(`${key}=${decodedValue}`); } else if (key.includes('param')) { // Add the param directly to the path segments pathSegments.push(decodedValue); } else { // Keep other parameters as query parameters otherParams.push(`${key}=${value}`); } }); // Construct the new URL const newPath = pathSegments.join('/'); const paramUTM = allUTM.join('&'); const otherQueryParams = otherParams.join('&'); let finalUrl = newPath; if (paramUTM) { finalUrl += `?${paramUTM}`; } if (otherQueryParams) { finalUrl += `${paramUTM ? '&' : '?'}${otherQueryParams}`; } return finalUrl; } /** * Adjusts the URL for mobile devices. * @param {string} url The original URL. * @returns {string} The adjusted URL for mobile devices. */ function adjustUrlForMobile(url) { return url; } /** * Updates the button href with the new URL, ensuring the domain remains as 'https://my.telkomsel.com/app/'. * @param {string} newUrl The new URL to set as the button's href. */ function updateButtonHref(newUrl) { const fixedUrl = newUrl.replace(/^mytelkomsel:\/\/app\//, 'https://my.telkomsel.com/app/'); if (document.getElementById('button-1')) document.getElementById('button-1').href = fixedUrl; } /** * Triggers auto redirect event for GTM. * @param {string} [param='apps'] The parameter to use in the GTM event. */ function autoRedirectGTM(param = 'apps') { dataLayer.push({ event: `auto_redirect_continue_to_${param}`, event_category: 'Interstitial Page', custom_campaign: `/${frontUrl.split('/')[4]}` }); } /** * Handles automatic redirection based on the 'force' and 'redirect' parameters. * @param {string} newUrl The URL to redirect to. */ function handleAutoRedirect(newUrl) { if (!redirect) return; // Only redirect if redirect=true if (force === 'web') { const anchor = document.createElement('a'); anchor.href = newUrl; autoRedirectGTM('website'); setTimeout(() => { anchor.click(); }, 1500); } else { const anchor = document.createElement('a'); anchor.href = newUrl; autoRedirectGTM(); setTimeout(() => { anchor.click(); }, 1500); } } // Main execution var newUrl = buildNewUrlFromQueryParams(); updateButtonHref(newUrl); handleAutoRedirect(newUrl);