Analytics
Smart WebView supports integration with Google Analytics using the gtag.js library to track user interactions within your web content loaded in the app.
Configuration
-
Get Your Measurement ID: Obtain your Google Analytics Measurement ID (format
G-XXXXXXXXXX
for GA4 orUA-XXXXXXXXX-Y
for Universal Analytics) from your Google Analytics property settings. -
Set the ID in Configuration:
Assign your Measurement ID to the `ASWV_GTAG` variable in `SmartWebView.java`: ```java // Your Google Analytics Measurement ID static String ASWV_GTAG = "YOUR_GTAG_ID"; // <-- Replace with your actual ID ``` If `ASWV_GTAG` is left empty, Analytics integration will be disabled. ```md ```
How it Works
- Dynamic Injection: Instead of adding the gtag.js snippet directly to your web page’s HTML, Smart WebView injects it dynamically using JavaScript after the page has finished loading.
This is handled by the onPageFinished
event inMainActivity.java
’sCallback
, which calls theFunctions.inject_gtag
method. This method executes JavaScript in the WebView to load and initialize gtag.js with your configuredASWV_GTAG
ID.iOS would use the webView(_:didFinish:)
delegate method (WKNavigationDelegate) to trigger JavaScript evaluation (evaluateJavaScript(_:completionHandler:)
) that injects and initializes gtag.js. - Improved Performance: This dynamic injection approach prevents the Analytics script from potentially blocking initial page rendering and ensures more reliable tracking.
Tracking Events
Once gtag.js is loaded, you can track events within your web application’s JavaScript code just as you would on a regular website. This part is platform-agnostic.
Example: Tracking a Button Click
// In your web page's JavaScript (e.g., script.js)
document.getElementById('myButton').addEventListener('click', function() {
// Check if gtag function exists (it should after injection)
if (typeof gtag === 'function') {
gtag('event', 'button_click', {
'event_category': 'Engagement',
'event_label': 'Special Feature Button',
'value': 1
});
console.log('GA event sent: button_click');
} else {
console.error('gtag function not found. Analytics might not be initialized.');
}
});
Refer to the Google Analytics gtag.js documentation for details on tracking events.
Identifying App Traffic
The User-Agent string modification feature can help distinguish traffic coming from your Smart WebView app.
POSTFIX_USER_AGENT
is enabled (default), the USER_AGENT_POSTFIX
(default: “SWVAndroid”) will be appended to the User-Agent string.
md <!-- A similar mechanism can be implemented on iOS to modify the WKWebView's user agent string. Details here. -->