Files
landing_page/qdrant-landing/themes/qdrant-2024/assets/js/helpers.js
T
jasonbryant84 df33a61423 Cloud UI Outbound Link URL Param (#1055)
* adding url param 'qdrant-tech' to outbound links to Cloud UI for the sake of tracking user journey (when consented)

* tagCloudUILinksWithHash() should not be conditional but occur before tracking events/elements are registered

* tagCloudUILinksWithHash within DOMContentLoaded and before registering All anchors for tracking

* import correction

* add new key/value pair to interaction

* uniform naming convention and remove "s" from key, and CROSS_SITE_URL_PARAM_KEY for uniformity
2024-07-29 21:39:05 +02:00

107 lines
3.1 KiB
JavaScript

export function isElementInViewport(el) {
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
export function scrollIntoViewWithOffset(id, offset) {
offset = offset || 0;
const targetPosition =
document.getElementById(id).getBoundingClientRect().top - document.body.getBoundingClientRect().top - offset;
window.scrollTo({
top: targetPosition,
behavior: 'smooth',
});
}
export function isNodeList(list) {
return Object.prototype.isPrototypeOf.call(NodeList.prototype, list);
}
export function initGoToTopButton(selector) {
const button = document.querySelector(selector || '.go-to-top');
if (!button) {
return;
}
window.addEventListener('scroll', () => {
const shouldShow = window.scrollY > window.innerHeight / 3;
button.classList.toggle('d-block', shouldShow);
button.classList.toggle('d-none', !shouldShow);
});
button.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
// GET COOKIE
export function getCookie(name) {
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
name = name + "=";
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
}
// SET COOKIE
export function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
const expires = 'expires=' + date.toUTCString();
document.cookie = name + '=' + value + ';' + expires + ';path=/;Secure';
}
// Logging in Development Mode (localhost)
export function devLog(str) {
if (window.location.host.includes('localhost')) {
console.log(str)
}
}
export const CROSS_SITE_URL_PARAM_KEY = 'qdrant-tech';
export function tagCloudUILinksWithHash() {
const targetUrl = 'https://cloud.qdrant.io/';
// Generate a random 32-character string
const generateRandomString = (length = 32) => {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
return Array.from({ length }, () => charset.charAt(Math.floor(Math.random() * charset.length))).join('');
};
const parameterValue = generateRandomString();
// Function to add or update query parameter in the URL
function addOrUpdateQueryParam(url, paramName, paramValue) {
const urlObj = new URL(url, window.location.origin); // Ensures URL is absolute
urlObj.searchParams.set(paramName, paramValue);
return urlObj.toString();
}
// Select all <a> elements with href exactly containing targetUrl
const links = document.querySelectorAll(`a[href*="${targetUrl}"]`);
// Loop through all selected <a> elements and update their href
links.forEach(link => {
link.href = addOrUpdateQueryParam(link.href, CROSS_SITE_URL_PARAM_KEY, parameterValue);
});
}