Home >Web Front-end >JS Tutorial >How to Track Outbound Links in Google Analytics

How to Track Outbound Links in Google Analytics

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-21 12:30:15179browse

How to Track Outbound Links in Google Analytics

Key Points

  • Google Analytics does not automatically track external links, but users can add this feature by upgrading to Universal Analytics and implementing custom event tracking codes.
  • Custom event tracking code Use JavaScript to add a click event handler to the body element, then record external clicks and send their data to Google Analytics. It is recommended to use jQuery 1.x or other libraries for powerful cross-browser event handling.
  • Tracking external links is crucial to understanding user behavior, optimizing website content, improving user experience, increasing engagement, and identifying potential collaboration opportunities with other websites. Users who don’t understand the code can use Google Tag Manager.

Google Analytics provides massive amounts of information. If you simply add tracking scripts to the page, you will face endless streams and reports about site user activity. However, while Analytics displays an exit page, it won't tell you which links the user clicked to leave your site. In this article, we will learn how to add external link tracking.

Does Google record external links?

Possible. If you use Analytics to link from one website to another using Analytics, Google may record this relationship. Unfortunately, if one or more outbound sites do not use Analytics, the report can be misleading. Google has other ways to collect data: When you have top browsers and search engines, you can collect a lot of statistics! However, we will then move from the website Analytics to a more vague territory; Google doesn't necessarily want to share that data. Fortunately, we can collect external link details ourselves.

First upgrade to Universal Analytics!

You must upgrade to Universal Analytics before we proceed. Google may have started this process for you, but the tracking code on your website page must be updated. This is cumbersome, but the external link tracking code shown below will not work without it. (It works with older versions of Analytics but will eventually stop working, so it's better to upgrade now.)

Custom event tracking

Analytics supports event tracking. Typically, it is used to record interactions controlled by JavaScript within a page, such as opening a widget or making an Ajax call. We can use event tracking to record external links, but we need to overcome some obstacles:

  • This event must be recorded on all browsers and will not hinder navigation;
  • We should not need to manually identify or attach a separate handler to each external link;
  • We have to make sure that events are logged before the external page starts loading.

Solution...

  1. We attach the click event handler to the body element. This will receive click link events as they bubbling through the DOM.
  2. We can detect if the link will open a page on a different domain than ours. If it is an external link, we will cancel the click event and start Analytics event tracking.
  3. In the background, Analytics sends data by requesting image beacons. After the call is finished, it can run the callback function so that we can redirect to the external page.
  4. We need to pay attention and make sure tracking never stops user navigation, even in the event of failure. The process must be fast, not handling clicks that have been deactivated by other processes, and ensure that the link works properly even if the Analytics event fails.

We want the tracking to work anywhere, so I recommend using a library with powerful cross-browser event handling. I'll use jQuery 1.x in this example, because most websites use it, but you can replace lightweight options like min.js, Zepto.js, Miniified.js, or your own event handler. The complete code is shown below. This can be added to an existing JavaScript file, or in a script block, as long as it is loaded somewhere in the HTML body (ideally, just before the end tag). jQuery (or your alternative) must be loaded first, although Google Analytics tracking code can appear anywhere on the page.

<code class="language-javascript">/* Track outbound links in Google Analytics */
(function($) {

  "use strict";

  // current page host
  var baseURI = window.location.host;

  // click event on body
  $("body").on("click", function(e) {

    // abandon if link already aborted or analytics is not available
    if (e.isDefaultPrevented() || typeof ga !== "function") return;

    // abandon if no active link or link within domain
    var link = $(e.target).closest("a");
    if (link.length != 1 || baseURI == link[0].host) return;

    // cancel event and record outbound link
    e.preventDefault();
    var href = link[0].href;
    ga('send', {
      'hitType': 'event',
      'eventCategory': 'outbound',
      'eventAction': 'link',
      'eventLabel': href,
      'hitCallback': loadPage
    });

    // redirect after one second if recording takes too long
    setTimeout(loadPage, 1000);

    // redirect to outbound page
    function loadPage() {
      document.location = href;
    }

  });

})(jQuery); // pass another library here if required</code>

This event is recorded using the category name "outbound" (outlink), the operation name "link" (link), and the value set as the external link URL. If necessary, you can modify these in the ga call (lines 24-26). After implementation, visit your website and click on some external links. You should see the activity in the Analytics Live > Events panel. After a few hours, more data will appear in the Behavior Events pane. Feel free to use this code.

Frequently Asked Questions about Tracking External Links in Google Analytics

What is the point of tracking external links in Google Analytics?

Tracking external links in Google Analytics is essential to understand user behavior on your website. It allows you to see which external links visitors click on, giving you insight into their interests and preferences. This data can be used to optimize website content, improve user experience, and increase engagement. It also helps identify potential collaboration opportunities with other websites your audience frequently visits.

How to set up Google Analytics to track external links?

Setting up Google Analytics to track external links involves creating and implementing custom event tracking codes. This code should be added to the HTML of each external link on the website. When a user clicks on a link, the event is recorded in Google Analytics. You can then access this data in the Events section of your Google Analytics account.

Can I track external links on the website without coding knowledge?

Yes, you can use Google Tag Manager to track external links without coding knowledge. This tool allows you to create and manage tracking tags without modifying the website code. You simply set a new tab for the external link click and configure a trigger to trigger when the user clicks the external link.

What is the difference between external links and internal links?

Outside links refer to links that direct users to other websites on the website, while internal links refer to links that direct users to your website on other websites. Tracking these two types of links is important for SEO and understanding user behavior.

How to use external link tracking data to improve my website?

Outline link tracking data can be used to identify the types of content that the audience is interested in. By knowing which external websites visitors often visit, you can adjust the content to match their interests. This can improve engagement and user retention.

Will tracking external links affect my website SEO?

Tracking external links will not directly affect your website SEO. However, the data obtained can be used to improve your content strategy, which can indirectly improve your SEO. It is also important to note that linking to high-quality related websites can improve your website’s credibility and search engine rankings.

How to track external links in Google Analytics in real time?

By setting up event tracking for external link clicks, real-time tracking of external links can be implemented in Google Analytics. Once set up, you can view live data in the "Real" section of your Google Analytics account.

Can I track external links on specific pages of the website?

Yes, you can track external links on specific pages by setting page-specific event tracking in Google Analytics. This allows you to understand the user behavior of individual pages and optimize them accordingly.

What are the common challenges of tracking external links? How can I overcome these challenges?

Some common challenges of tracking external links include setting up the tracking code correctly, interpreting data, and maintaining tracking settings as the website evolves. These challenges can be overcome by using tools such as Google Tag Manager, seeking help from professionals, or learning more about Google Analytics.

Can I track external links on my mobile device?

Yes, Google Analytics allows you to track external links on desktop and mobile devices. This provides a comprehensive view of user behavior across all devices.

The above is the detailed content of How to Track Outbound Links in Google Analytics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn