As in MAX's answer it's true, it's very difficult to manipulate the user-agent while keeping all the attributes, Like OS, and rendering engine etc...
At the sametime I still want to target my app users with a custom user-agent, and be able to separate traffic from this webview app.
What I did is this:
1- Setting the custom user-agent
Instead of replacing the whole user-agent with a custom one, I appended this to the user-agent [AppID/AppVersion], found great info from this blog: Webviews and User-Agent strings.
Now the user-agent looks something like this:
Mozilla/5.0 (Linux; Android 9; wv)
AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91
Mobile Safari/537.36 [Custom App/1.0.1]
Check: Correct way to format user-agent string in an Android WebView App?
2- Setting a custom dimension in Google Analytics
Since Google Analytics will mark all browser value visits from this agent as Android Webview, I went to assign a custom dimension to be able to identify the custom user-agent sessions and create a separate view for it.
In the backend with PHP I set the value of the dimension based on the user-agent.
<script>
<?php
if(strpos($_SERVER['HTTP_USER_AGENT'], 'Custom user agent here')!==false)
{
$customAgent_value = 'your agent';
}
?>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-', {
'custom_map': {'dimension1': 'custom_agent'}
});
gtag('event', 'custom_agent_event', {'custom_agent': '<?= $customAgent_value;?>'});
</script>
This is working fine for me now. I can target users from a specific webview app, and at the same time am able to separate the traffic from different webviews in Analytics.