问题描述
我想在非基于网络的应用程序(基于Windows的应用程序)中向Google分析发送请求?
我试过以下方法。
public string trackingId =UA-XXXXXXXX-2;
private void button1_Click(object sender,EventArgs e)
{
string shopname =ShopTestng;
字符串pagename =Testing_MyApp;
callAnalyticsmethod2(pagename,shopname);
}
private void callAnalyticsmethod2(string pageName,string shopname)
{
//在计算机名称上创建哈希码7用户名
string visitorId = GetUniqueUserId();
if(string.IsNullOrEmpty(pageName))
pageName = visitorId;
字符串utmGifLocation =http://www.google-analytics.com/__utm.gif;
string GifUrl =utmwv = 4.9+
& utmn =+ GetRandomNumber()+
& utmp =+ pageName +
& utmac =+ trackingId +
& utmcc = __ utma%3D999.999.999.999.999.1%3B+
& utmvid =+ visitorId; // +? +
string shop = shopname.Replace(,_);
string addipara = GifUrl +& utmr = http://+ shop;
byte [] dataStream = Encoding.UTF8.GetBytes(addipara);
字符串请求= utmGifLocation;
WebRequest webRequest = WebRequest.Create(request);
webRequest.Method =POST;
webRequest.ContentType =application / x-www-form-urlencoded;
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
//发送数据。
newStream.Write(dataStream,0,dataStream.Length);
newStream.Close();
WebResponse webResponse = webRequest.GetResponse();
MessageBox.Show((((HttpWebResponse)webResponse).StatusDescription));
newStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(newStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
reader.Close();
newStream.Close();
webResponse.Close();
}
根据以上代码示例
$ b ((((HttpWebResponse)webResponse).StatusDescription)); $ b
MessageBox.Show
行显示为OK。但是当我检查谷歌分析时,访问计数不会增加。这是什么原因?
是否存在我缺少的东西或任何其他方式向分析发送请求?
(并根据我的场景进行编辑。
private void analyticsmethod4(字符串trackingId,字符串页面名称)
{
Random rnd = new Random();
long timestampFirstRun,timestampLastRun,timestampCurrentRun,numberOfRuns;
//获取第一次运行时间
timestampFirstRun = DateTime.Now.Ticks;
timestampLastRun = DateTime.Now.Ticks-5;
timestampCurrentRun = 45;
numberOfRuns = 2;
//有些值需要
字符串domainHash =123456789; //这可以为您的域在线计算
int uniqueVisitorId = rnd.Next(100000000,999999999); // Random
string source =Shop;
string medium =medium123;
string sessionNumber =1;
字符串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 + //帐号
& 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;
使用(var client = new WebClient())
{
client.DownloadData(statsRequest);
//流数据= client.OpenRead(statsRequest);
// StreamReader reader = new StreamReader(data);
// string s = reader.ReadToEnd();
}
}
about -
i want to send request to Google analytics in non web based application(Windows based application) ?i tried following method.
public string trackingId = "UA-XXXXXXXX-2";
private void button1_Click(object sender, EventArgs e)
{
string shopname = "ShopTestng";
string pagename="Testing_MyApp";
callAnalyticsmethod2(pagename, shopname);
}
private void callAnalyticsmethod2(string pageName, string shopname)
{
// create hash code base on pc name 7 user name
string visitorId = GetUniqueUserId();
if (string.IsNullOrEmpty(pageName))
pageName = visitorId;
string utmGifLocation = "http://www.google-analytics.com/__utm.gif";
string GifUrl = "utmwv=4.9" +
"&utmn=" + GetRandomNumber() +
"&utmp=" + pageName +
"&utmac=" + trackingId +
"&utmcc=__utma%3D999.999.999.999.999.1%3B" +
"&utmvid=" + visitorId;// + "?" +
string shop = shopname.Replace(" ", "_");
string addipara = GifUrl+"&utmr=http://" + shop;
byte[] dataStream = Encoding.UTF8.GetBytes(addipara);
string request = utmGifLocation;
WebRequest webRequest = WebRequest.Create(request);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
// Send the data.
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
WebResponse webResponse = webRequest.GetResponse();
MessageBox.Show((((HttpWebResponse)webResponse).StatusDescription));
newStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(newStream);
string responseFromServer = reader.ReadToEnd();
MessageBox.Show(responseFromServer);
reader.Close();
newStream.Close();
webResponse.Close();
}
according to the above code sample
MessageBox.Show((((HttpWebResponse)webResponse).StatusDescription));
line display as "OK". but when i check Google analytics, visit count doesn't increase. what is the reason for this?is there something i missing or any other way to send request to analytics?
i found similar answer from SOF (Cause Google Analytics log from non-web application and editted it according to my scenario.
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();
}
}
I found good Articles about -
Google Analytics for Desktop Application
这篇关于如何在非基于Web的应用程序中向Google Analytics发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!