Frustrated by CMS restrictions or lack of access when it comes to managing canonical tags? Fear not, fellow SEO adventurers! Google Tag Manager (GTM) has your back, offering a handy way to override and manage canonical tags with ease.
Why Override Canonical Tags with GTM?
- Consolidate Page Equity: Direct search engines to the most authoritative version of similar pages, boosting their rankings.
- Temporarily Remove Pages: Hide pages from search results for maintenance or updates without messing with your CMS.
- Combat Keyword Cannibalization: Prevent near-duplicate content from competing for the same keywords, ensuring the right pages shine.
Step-by-Step Guide:
- Create a Lookup Table:
- In GTM, navigate to “Variables” and click “New.”
- Select “Lookup Table” as the variable type.
- Enter your source URLs (pages to be overridden) and their corresponding destination URLs (the canonical pages).
2. Craft a Custom HTML Tag:
- Head to “Tags” and click “New.”Choose “Custom HTML” as the tag type.Paste the following code, replacing placeholder text with your lookup table variable:
JavaScript<script> (function() { var canonicalUrl = {{Lookup Table - Canonical URLs}}; if (canonicalUrl) { document.querySelector('link[rel="canonical"]').href = canonicalUrl; } else { var canonicalLink = document.createElement('link'); canonicalLink.rel = 'canonical'; canonicalLink.href = '{{Page URL}}'; // Default to current page URL if no match document.head.appendChild(canonicalLink); } })(); </script>
3. Assign a Universal Trigger:
- Set the firing trigger for your custom HTML tag to “All Pages – Page View.” This ensures it fires on every page.
4. Publish and Test:
- Publish your GTM container for the changes to take effect.
- Use GTM’s preview mode and a crawler tool like Screaming Frog to verify that the canonical tags are working correctly.
Troubleshooting Tips:
- Double-check your lookup table for accuracy.
- Ensure the custom HTML tag is firing as expected.
- Consider using a crawler tool to identify any remaining issues.
By mastering this technique, you’ll gain effortless control over your website’s canonical tags, even when faced with CMS limitations. Happy SEO trailblazing!