How to track ASP.Net MVC web app using Google Analytics when multiple subdomains are used

∥☆過路亽.° 提交于 2020-01-25 06:45:13

问题


Brief Introduction of ASP.Net MVC 5 webapp I work on

  • This ONE Webapp serves many different websites (kind of). I mean code is just ONE but it runs for our 100's of customers and they all see their website differently (as if they all have their separate website)
  • But in the back end its all handled from the just ONE code. We kind of read the subdomain and based on that we load the website/customer specific BLOB and load website specific to a particular customer.
  • Long story short if I STOP the IIS for my standalone webapp all the website will stop.

URl's for the website looks like below:

Example:

  1. A.abc.xyx.com - website for customer 1
  2. B.abc.xyz.com - website for customer 2
  3. C.abc.xyz.com - website for customer 3

Now one of my customer is asking for google analytics to be added to their website. So I went to google analytics and added A.abc.xyx.com website. It generted one JS Tag.

I got the code and added it in my Head Tag. And published the website.

Problem: That tag is now present in all the customer's website.

For example:

if A.abc.xyz.com - opened 5 times

if B.abc.xyz.com - opened 3 times

if C.abc.xyz.com - opened 2 times

The google analytics shows me that active connection = 10.

  • I know that all the websites have the same code and thus all have the same Google analytics Tracking code.

  • My Assumption was since I created the tag for A.abc.xyz.com, it will only track that. But its tracking all the websites. WHY SO???

Workaround:

I can try to read the subdomain that is getting opened in the browser and CONDITIONALLY add the Tracking script only when its matches a particular subdomain. But tomorrow if some other customer asks for their standalone Analytics how will I manage it.

Question: How to make sure that only A.abc.xyx.com gets tracked


回答1:


You could create a sub folder for each domain e.g.

/custom/A /custom/B

etc. Then put a partial view inside there for the analytics e.g.

/custom/A/analytics.cshtml

Then in your Layout Page:

string domain = "logic to get domain here";
if (File.Exists(Server.MapPath(string.Format("/custom/{0}/analytics.cshtml", domain))))
{
    Html.RenderPartial(string.Format("/custom/{0}/analytics.cshtml", domain));
}

That way if there is a file for that customer if will include it, otherwise it won't.



来源:https://stackoverflow.com/questions/49934301/how-to-track-asp-net-mvc-web-app-using-google-analytics-when-multiple-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!