Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active November 21, 2024 08:21
Show Gist options
  • 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>.
I did not know at the time of writing this code and recording this video (https://www.youtube.com/watch?v=MHKzHf1BhrY) that THIS IS POSSIBLE NATIVELY FROM GHL, NO CUSTOM CODE NEEDED, NO CUSTOM VALUE NEEDED!
Please use GHL Native functionality as documented here: https://help.gohighlevel.com/support/solutions/articles/48001172004
<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>
@QuinnMarketing
Copy link

You are a weapon! Thank you for letting me know!

@cliffordp
Copy link
Author

@QuinnMarketing tyvm, yw. request any additional learning topics at https://tourkick.com/contact

@cliffordp
Copy link
Author

yw @JuliaBland :)

@MariahHughes
Copy link

MariahHughes commented Nov 13, 2024

Thanks for sharing it with us :) I’m applying for a PhD program in Sociology, and writing a personal statement was a daunting task. I used https://academized.com/write-my-personal-statement and their service was outstanding. The writer crafted a statement that was professional, well-structured, and tailored to the program’s requirements. It highlighted my academic achievements and research interests perfectly. I’m really impressed with their work and would recommend them to anyone looking for help with applications!

@cliffordp
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment