Shopify themes, liquid, logos, and UX
Hi Shopify Community,
I hope you are all doing well! I'm facing an issue with dynamically updating the heading on my product page based on a UTM parameter in the URL. I've implemented the following JavaScript code, but it's causing the heading to be duplicated:
<script>
document.addEventListener('DOMContentLoaded', function() {
// Function to get UTM parameter from URL
function getUTMParameter(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
// Get the 'utm_content' parameter from the URL
const keyword = getUTMParameter('utm_content');
// Check if the keyword is present and update the product title
if (keyword) {
const titleElement = document.querySelector('.product__title'); // Adjust the selector accordingly
if (titleElement) {
const originalTitle = titleElement.textContent;
// Check if the title hasn't been modified already
if (!originalTitle.includes(keyword)) {
// Create a new h1 element, update its content, and replace the existing title element
const newTitleElement = document.createElement('h1');
newTitleElement.textContent = originalTitle + ' - ' + keyword;
titleElement.parentNode.replaceChild(newTitleElement, titleElement);
}
}
}
});
</script>
I've tried different versions of the code, but the issue persists. The heading appears duplicated. Can anyone provide insights into what might be causing this issue or suggest an alternative approach?
Any help or guidance would be highly appreciated. Thank you!
Solved! Go to the solution
This is an accepted solution.
ok found the issue.
You need to replace this line.
const titleElement = document.querySelector('.product__title'); // Adjust the selector accordingly
with
const titleElement = document.querySelector('.product__title h1'); // Adjust the selector accordingly
<script>
document.addEventListener('DOMContentLoaded', function() {
// Function to get UTM parameter from URL
function getUTMParameter(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
// Get the 'utm_content' parameter from the URL
const keyword = getUTMParameter('utm_content');
// Check if the keyword is present and update the product title
if (keyword) {
const titleElement = document.querySelector('.product__title'); // Adjust the selector accordingly
if (titleElement) {
const originalTitle = titleElement.textContent;
// Check if the title hasn't been modified already
if (!originalTitle.includes(keyword)) {
titleElement.textContent = originalTitle + ' - ' + keyword;
}
}
}
});
</script>
I tried that before but then it comes out like this:
including some padding which was already there i forgot to mention, need toget rid of that to.
Replace
titleElement.textContent = originalTitle + ' - ' + keyword;
with
titleElement.textContent = keyword;
That removes the whole title and only shows the keyword in paragraph
This is an accepted solution.
ok found the issue.
You need to replace this line.
const titleElement = document.querySelector('.product__title'); // Adjust the selector accordingly
with
const titleElement = document.querySelector('.product__title h1'); // Adjust the selector accordingly
No way man! How did u find that, I was testing for like 4 hours?? Thank you so so much, u got it 😂
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024