Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active December 19, 2023 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/9856cdd1497802996f472d3d31299026 to your computer and use it in GitHub Desktop.
Save cliffordp/9856cdd1497802996f472d3d31299026 to your computer and use it in GitHub Desktop.
Go High Level funnels and websites: replace/insert text of any `.replace_location` class with the `location` URL query parameter. Works in <head> or <body>.
<script id="tourkick-ghl-url-query-param-replacement">
// Source: https://gist.github.com/cliffordp/9856cdd1497802996f472d3d31299026
// Get and decode the query parameter from the URL.
function getQueryParam( param ) {
const urlParams = new URLSearchParams( window.location.search );
const paramValue = urlParams.get( param );
// Return null if the parameter is not present or is an empty string.
return (paramValue !== null && paramValue.trim() !== '') ? decodeURIComponent( paramValue ) : null;
}
// Replace or insert the text content of all children that are text nodes.
function replaceTextContent( element, newText ) {
if ( element.hasChildNodes() ) {
element.childNodes.forEach( function ( child ) {
if ( child.nodeType === Node.TEXT_NODE ) {
child.textContent = newText; // Replace text of text nodes.
} else if ( child.nodeType === Node.ELEMENT_NODE ) {
if ( !child.hasChildNodes() ) {
// If the child element is empty, add a text node.
child.appendChild( document.createTextNode( newText ) );
} else {
// Recursive call for non-empty child elements
replaceTextContent( child, newText );
}
}
} );
} else if ( element.nodeType === Node.ELEMENT_NODE ) {
// If the element itself is empty, add a text node
element.appendChild( document.createTextNode( newText ) );
}
}
// Replace the text content of all elements with a specific class, including itself.
function replaceContentWithQueryParam( className, queryParam ) {
const elements = document.querySelectorAll( '.' + className );
const queryParamValue = getQueryParam( queryParam );
// Check if queryParamValue is neither null nor an empty string.
if ( queryParamValue ) {
elements.forEach( function ( element ) {
replaceTextContent( element, queryParamValue );
} );
console.log( `Replaced content in ${elements.length} elements.` );
} else {
console.log( 'No valid query parameter value found for: ', queryParam );
}
}
// Instantiate a MutationObserver to wait for changes and update text. Required for GHL because it uses Nuxt (Vue.js).
const observer = new MutationObserver( function ( mutations ) {
mutations.forEach( function ( mutation ) {
if ( document.querySelector( '.replace_location' ) ) {
observer.disconnect(); // Stop observing once we make the changes.
replaceContentWithQueryParam( 'replace_location', 'location' );
}
} );
} );
// Observer configuration.
const config = { childList: true, subtree: true };
// Start observing the target node for configured mutations.
observer.observe( document.body, config );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment