如何使用Google Tag Manager(GTM)追踪链接点击及广告活动用户来源
Alright, let's tackle your Google Tag Manager setup to track those specific link clicks and identify which one brought users to your landing page. I’ve built similar tracking setups for campaign attribution before, so this approach should cover all your requirements.
We need two key components here:
- Track when users click your target campaign links
- Store the clicked link’s source information (via a cookie) so we can retrieve it when the user lands on your site
Since all links open your landing page, the cookie will carry the source data from the click event to the landing page load.
First, let’s build a variable that recognizes exactly which link was clicked:
- In your GTM workspace, go to Variables > User-Defined Variables > New
- Select Custom JavaScript as the variable type
- Paste this code (adjust the fixed URLs to match your exact links):
function() { const clickUrl = {{Click URL}}; // Match fixed campaign links if (clickUrl.includes("www.example.com/twitter")) return "twitter"; if (clickUrl.includes("www.example.com/whatsapp")) return "whatsapp"; if (clickUrl.includes("www.example.com/linkdIn")) return "linkedin"; // Match dynamic source links (e.g., www.example.com/facebook-ad-q3) const sourceRegex = /www\.example\.com\/([a-zA-Z0-9_-]+)/; const sourceMatch = clickUrl.match(sourceRegex); if (sourceMatch && sourceMatch[1]) { return sourceMatch[1]; } // Fallback if no target link matches return "unknown_source"; }
- Name this variable something clear like
Link Source - Campaign
Next, we need a trigger that only fires when users click your target links:
- Go to Triggers > New
- Choose Just Links as the trigger type
- Under This trigger fires on, select Some Link Clicks
- Add this condition:
Link Source - Campaigndoes not equalunknown_source
- Name this trigger
Campaign Link Clicks
We’ll save the clicked source to a cookie so we can access it on the landing page. Create a Custom HTML tag:
- Go to Tags > New
- Select Custom HTML as the tag type
- Paste this code to set a cookie that lasts 1 day (adjust
max-ageif you need a longer/shorter window):
<script> document.cookie = `campaign_entry_source={{Link Source - Campaign}}; path=/; max-age=86400; SameSite=Lax`; </script>
- Under Triggering, select your
Campaign Link Clickstrigger - Name this tag
Set Campaign Source Cookie
Now, when users land on your page, we’ll read the cookie and send that source data to your analytics tool (I’ll use GA4 as an example):
First, create a variable to read the cookie:
- Go to Variables > User-Defined Variables > New
- Select Custom JavaScript
- Paste this code:
function() { const cookieName = "campaign_entry_source="; const decodedCookie = decodeURIComponent(document.cookie); const cookieArray = decodedCookie.split(';'); for (let i = 0; i < cookieArray.length; i++) { let cookie = cookieArray[i]; while (cookie.charAt(0) === ' ') { cookie = cookie.substring(1); } if (cookie.indexOf(cookieName) === 0) { return cookie.substring(cookieName.length, cookie.length); } } // Fallback if no cookie exists (direct traffic) return "direct"; }
- Name this variable
Entry Source - Cookie
Then, create a GA4 Event Tag to send the source:
- Go to Tags > New
- Choose Google Analytics: GA4 Event
- Enter your GA4 Measurement ID
- Set the Event Name to
campaign_entry - Add an event parameter:
- Parameter name:
source - Value:
{{Entry Source - Cookie}}
- Parameter name:
- Under Triggering, select the Page View trigger (or a specific landing page trigger if you only want to track this on that page)
- Name this tag
GA4 - Track Campaign Entry Source
Before publishing, use GTM’s Preview mode to validate:
- Click Preview in your GTM workspace
- Navigate to the page with your campaign links
- Click each link (twitter, whatsapp, linkedin, and a dynamic source link)
- Confirm the
Set Campaign Source Cookietag fires for every valid click - Load your landing page and check that the
GA4 - Track Campaign Entry Sourcetag fires, with the correctsourceparameter - Verify the
campaign_entryevents appear in your GA4 Realtime report with the right source values
If you want to clear the cookie after tracking the entry (to avoid affecting future sessions), add another Custom HTML tag that fires on landing page load:
<script> // Delete the cookie by setting max-age to 0 document.cookie = "campaign_entry_source=; path=/; max-age=0"; </script>
Trigger this tag with the same landing page trigger as your GA4 entry tag.
内容的提问来源于stack exchange,提问作者YahYa

