Sunday, May 19, 2024
HomeProgrammingHow to redirect with JavaScript without negatively impacting Google Analytics?

How to redirect with JavaScript without negatively impacting Google Analytics?

For redirects between mobile sites and PC sites, or redirects due to URL changes, there are occasional cases where redirects are performed using JavaScript because it is technically not possible to redirect on the server side using .htaccess, etc. . Although these sites seem to work without problems, in fact, fatal problems can be found when access analysis is performed using Google Analytics.

thumbnail 15

In this article, I will introduce the methods to solve those problems while pointing out the problems that occur in situations where you have no choice but to redirect with JavaScript. That being said, it’s not that there’s no problem once you do this, so try to do server-side redirects whenever possible.


Simple JavaScript redirect method, problems, and confirmation method

How to implement a simple JavaScript redirect

When trying to easily implement a redirect between a mobile site and a PC site with JavaScript,

// PCサイト側のJavaScript
if ( isMobileDevice() ) {
  location.href = "http://example.com/sp/index.html";
}

It will be implemented in the form of Due to its simplicity, this method is often used.

what is the problem

Problem 1: Since the referrer information is your site, it will be direct inflow in Google Analytics

Although there is no problem from the user’s point of view, the referrer information will be your own domain. Also, in the latest universal analytics, inflows from your own domain are treated as direct inflows by default. As a result, when page A is opened by a search engine or other site inflow, if it is redirected to page B by JavaScript redirect by the above implementation, it will be a session that landed on page B, but the inflow route is search engine or other site. It will be treated as “direct inflow” instead of inflow from other sites. Depending on the timing of the redirect and the timing of Google Analytics pageview transmission, there is a possibility that the pageview will be sent twice before and after the redirect.

Issue 2: Missing gclid parameter for custom campaigns and AdWords

The above implementation method discards the parameter gclid that is given when you intentionally assign a custom campaign or link AdWords and Google Analytics when redirecting. As a result, although there is inflow to the site by allocating the custom campaign, the inflow with the corresponding custom campaign is underestimated. This makes it impossible to correctly measure inflows via campaigns.

How can I check if I have a problem implementation?

Please check the implementation of the actual site based on the following confirmation method. If there is a possibility that there is a problem, I think you should seriously consider renovating the site.

How to check problem 1

To check this, search Google, Yahoo search results, and other miscellaneous sites for links to URLs that should cause redirects. Then click on that link, check the real-time view of Google Analytics for the redirected inflow, and if you can confirm the inflow as the relevant referral source, there is no problem.

How to confirm Problem 2

On your own site, open the page where the redirect occurs. However, at this time, the URL is slightly crafted.

Add a custom campaign such as “?utm source=Manual&utm medium=Manual&utm_campaign=RedirectTest” after the normal URL and access the site. Look at the URL field after the redirect occurs. Check if the custom campaign you just added is still there. Alternatively, if you can check the inflow with the custom campaign when you accessed earlier in the real-time view of Google Analytics, there is no problem. If you can’t confirm the above inflow, there is a high possibility that it is an inappropriate implementation.

Proper redirect method and GA implementation of redirect destination page

Redirect method

The important point is to pass the information required by the redirect destination correctly when redirecting. In order to solve the two problems introduced above, the information required at the redirect destination is the referrer information when the user entered the page before the redirect and the parameter information used when opening the page before the redirect. will be

Writing specific code,

if ( isMobileDevice()) {
  var redirect_url = "http://example.com/sp/index.html" + location.search;
  if (document.referrer) {
    var referrer = "referrer=" + encodeURIComponent(document.referrer);
    redirect_url = redirect_url + (location.search ? '&' : '?') + referrer;
  }
  location.href = redirect_url;
}

becomes. As a result, the parameter information used when accessing the page before the redirect is maintained, and by adding the referrer information to the new parameter, the redirect destination can be notified of the original referrer information. .

Google Analytics on redirected page

Parameters such as Custom Campaigns and Custom Campaigns for AdWords work well with the above redirects alone. However, the above method alone is not sufficient for referrer information. To deal with referrer information, you’ll also need to customize Google Analytics on the redirected page.

ga('create', 'UA-0123456-1');
if (location.search.indexOf('referrer=') >= 0) {
  var params = location.search.replace('?', '').split('&');
  for (var i = 0; i < params.length; i++) {
    var kv = params[i].split('=');
    if (kv.length == 2 && kv[0] == 'referrer') {
      ga('set', 'referrer', decodeURIComponent(kv[1]));
      break;
    }
  }
}
ga('send', 'pageview');

Include the 10 lines starting with if above between generating the tracker and sending the pageview, like this: This extracts the referrer information if the referrer information is included in the URL and overwrites the referrer data sent by Google Analytics with that value.

Lastly, if you only use the above, the URL measured by Google Analytics will have a referrer parameter, which will be inconvenient when analyzing each page. Since you want to see the referrer parameter deleted in the URL on the Google Analytics management screen, please enter “referrer” in “Excluded URL query parameters” in the view settings of “Analytics settings” (if there is already something If so, include “referrer” separated by commas). As a result, the same pages with different referrers will be displayed together when viewing the URL such as “Action > Site Content > All Pages”.

summary

In the title, I wrote “when redirecting with JavaScript”, but in reality, even if it is a pattern that does not redirect with JavaScript, even if it is a tag refresh instruction or a redirect on the server side, custom It’s entirely possible that the campaign or gclid parameters are missing. Originally, the person who creates the site should understand the operation specifications of the access analysis tool and implement it appropriately, but those involved in web advertising / web marketing should once again consider the site they are in charge of. How about checking if it’s implemented properly?

RELATED ARTICLES

Leave a reply

Please enter your comment!
Please enter your name here

Recent Posts

Most Popular

Recent Comments