1

If I add a simple webbrowser control to a winforms app and then navigate to a page on my site, that page view is registered perfectly in Google Analytics.

But, if I do the same thing with the webbrowser in a console app it doesn't work.

The code I have is:

static void Capture(string url)
    {
        Thread thread = new Thread(delegate()
        {
            using (WebBrowser browser = new WebBrowser())
            {

                browser.ScrollBarsEnabled = false;
                browser.AllowNavigation = true;                 
                browser.Navigate(url);
                browser.Width = 1024;
                browser.Height = 768;
                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                while (browser.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
    }

    static void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser = sender as WebBrowser;
        Console.WriteLine(browser.DocumentText);
        System.Threading.Thread.Sleep(10000);
    }

It works, as in the console displays the html of the page i'm fetching, however the visit isn't registered in Analytics, presumably as it recognises it as a bot, or perhaps it's not executing the GA tracking code?

How can I modify it so it works like the Windows Forms version?

Thanks

Ben Durkin
  • 429
  • 1
  • 6
  • 20

0 Answers0