코드에서 Google 웹 로그 분석을 트리거하는 방법은 무엇입니까?
-
03-07-2019 - |
문제
내 앱은 내 웹 사이트에서 (html) 파일을 읽고 Google Analytics를 사용하여 해당 파일에 대한 액세스를 추적하고 싶습니다. 파일을 읽을 때 GA JavaScript가 실행되지 않으므로 추적되지 않습니다. 많은 종속성을 추가하지 않고 .NET 앱에서 javaScript를 실행하기 위해 GA를 코드에서 직접 트리거하는 방법이 있습니까?
해결책
Google Analytics는 JavaScript를 통해 Google 서버로 다시 웹 레퀴스트를 만들어 작동합니다. 프로그래밍 방식 으로이 작업을 수행하려면이 웹 요청을 직접 만들어야합니다. 브라우저에 페이지를로드 할 때 Fiddler 또는 Firebug를 사용하여 요청이 어떻게 보이는지 캡처합니다. 그런 다음 .NET 앱에서 동일한 URL을 사용할 수 있습니다.
다른 팁
최근 코드를 통해 Google Analytics에서 페이지보기를 기본적으로 로그인 할 수있는 .NET 라이브러리를 출시했습니다. 그것은 GNU에 따라 오픈 소스로 릴리스되므로 필요한 모든 것은 적절한 속성입니다.
여기에서 도서관을 얻을 수 있습니다. http://www.diaryofaninja.com/projects/details/ga-dot-net
예제 API 사용 :
GooglePageView pageView = new GooglePageView("My page title",
"www.mydomain.com",
"/my-page-url.html");
TrackingRequest request = new RequestFactory().BuildRequest(pageView);
GoogleTracking.FireTrackingEvent(request);
페이지에 추적 픽셀을 포함하여 추적 이벤트를 발사 할 수있는 HTTP 핸들러가 내장되어 있습니다.
<img src="/tracker.asmx?domain=mydomain.com&pagetitle=My%20Page%20Title&url=/my-page.aspx" />
또는 jQuery를 사용하여 Google Analytics (Zip, JPG 등)를 사용하여 페이지 내에서 링크를 추적 할 수 있습니다.
private void analyticsmethod4(string trackingId, string pagename)
{
Random rnd = new Random();
long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns;
// Get the first run time
timestampFirstRun = DateTime.Now.Ticks;
timestampLastRun = DateTime.Now.Ticks-5;
timestampCurrentRun = 45;
numberOfRuns = 2;
// Some values we need
string domainHash = "123456789"; // This can be calcualted for your domain online
int uniqueVisitorId = rnd.Next(100000000, 999999999); // Random
string source = "Shop";
string medium = "medium123";
string sessionNumber = "1";
string campaignNumber = "1";
string culture = Thread.CurrentThread.CurrentCulture.Name;
string screenRes = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height;
string statsRequest = "http://www.google-analytics.com/__utm.gif" +
"?utmwv=4.6.5" +
"&utmn=" + rnd.Next(100000000, 999999999) +
// "&utmhn=hostname.mydomain.com" +
"&utmcs=-" +
"&utmsr=" + screenRes +
"&utmsc=-" +
"&utmul=" + culture +
"&utmje=-" +
"&utmfl=-" +
"&utmdt=" + pagename +
"&utmhid=1943799692" +
"&utmr=0" +
"&utmp=" + pagename +
"&utmac=" +trackingId+ // Account number
"&utmcc=" +
"__utma%3D" + domainHash + "." + uniqueVisitorId + "." + timestampFirstRun + "." + timestampLastRun + "." + timestampCurrentRun + "." + numberOfRuns +
"%3B%2B__utmz%3D" + domainHash + "." + timestampCurrentRun + "." + sessionNumber + "." + campaignNumber + ".utmcsr%3D" + source + "%7Cutmccn%3D(" + medium + ")%7Cutmcmd%3D" + medium + "%7Cutmcct%3D%2Fd31AaOM%3B";
using (var client = new WebClient())
{
client.DownloadData(statsRequest);
//Stream data = client.OpenRead(statsRequest);
//StreamReader reader = new StreamReader(data);
//string s = reader.ReadToEnd();
}
}
이것을 참조하십시오 - http://tilr.blogspot.com/2012/10/google-analytics-use-google-analytics.html
Google Analytics는 사용자 정의 액션, 이벤트 또는 다루는 모든 것을 추적하는 두 가지 방법을 제공합니다. 귀하의 경우, 사소한 솔루션은 응용 프로그램에서 읽은 HTML 파일에 대한 가상 페이지 뷰를 생성하는 것입니다. 자바 스크립트 기능:
pageTracker._trackPageview("/Foo.html");
이런 식으로 매번 foo.html 처리 된 경우, PageView는 응용 프로그램의 일반 쿼리와 동일하게 PageView가 생성됩니다.
이것을 구별하고 싶다면 foo.html정상적인 페이지 뷰에서 GA에는 멋진 기능이 있습니다. 이벤트 추적 그런 다음 살펴 봐야합니다.
I ended up using the WebBrowser component to load the .html file, and thereby trigger the GA tracker. The WebBrowser component executes the embedded JavaScript.
using (WebBrowser wb = new WebBrowser())
{
wb.Url = new Uri(@"mytrackingpage.html");
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
}
Now all I have to do is to add some errorhandling, get rid of the ugly DoEvents and move the WebBrowser to a separate thread.
Google has libraries (in alpha) for several different languages for accessing various google APIs, including Analytics -- there's a nice description here -- https://developers.google.com/analytics/devguides/collection/ and the .NET library is https://developers.google.com/api-client-library/dotnet/apis/analytics/v3
Also keep in mind their privacy policy you have to adhere to when using this: https://developers.google.com/analytics/devguides/collection/protocol/policy