1.Background
The code snippets in sections 2.1 and 2.2 below aim at executing Google Analytics JavaScript from a C# console application that uses a headless web browser to load the following HTML page hosted on a web server (let’s say https://my.server.com/index.html):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title Placeholder</title>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-**********"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag()
{
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-**********');
</script>
</head>
<body>Body Placeholder
</body>
</html>
When the above mentioned HTML page is loaded from a web browser such as Google Chrome, its Google Analytics JavaScript gets executed as expected (Google Analytics server reports that this web page was loaded).
2. C# Code snippets
2.1. Using Puppeteer Sharp
using System.Threading.Tasks;
using PuppeteerSharp;
class Program {
static async Task Main(string[] args) {
await new BrowserFetcher().DownloadAsync();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions {Headless = true});
var page = await browser.NewPageAsync();
page.DefaultNavigationTimeout = 0;
page.DefaultTimeout = 0;
var navigation = new NavigationOptions {
Timeout = 0,
WaitUntil = new[] {
WaitUntilNavigation.DOMContentLoaded,
WaitUntilNavigation.Networkidle0
}
};
await page.GoToAsync("https://my.server.com/", navigation);
await browser.CloseAsync();
}
}
2.2 Using CefSharp
using CefSharp;
using CefSharp.WinForms;
class Program {
static void Main(string[] args) {
var settings = new CefSettings();
Cef.Initialize(settings);
var browser = new ChromiumWebBrowser("https://my.server.com/");
Cef.Shutdown();
}
}
3. Question
Why is the Google Analytics JavaScript of the https://my.server.com/index.html page not executed (i.e. Google Analytics server does not report that this web page was loaded) when running the Main
methods in sections 2.1 or 2.2
?