How to set custom user-agent that Google Analytics can read

Viewed 4481

I want to set a custom user-agent for a webview app that embeds my website. I am able to set a custom agent like this ("My App Android"). The issue is that Google Analytics reads traffic as Desktop for this agent not mobile like regular webview. What's the best method to set a custom user-agent while still keeping data like mobile, and Device OS so tools like Google Analytics can still read it.

2 Answers

You can manipulate the User Agent but you can't control how Google will interpret the resulting device/OS:

  • The processing is done on the server side (Google) so there is no way of directly modifying that data (even when sending data via the measurement protocol).

  • The processing details are not disclosed by Google so you won't know what the outcome of your experiments are until they're reported by Google Analytics (which due to the 24-48 hour data processing latency might make such experimentation tedious).

  • Attempting to manipulate it might "break" your analytics: Google is vague about this, they just say: "Google has libraries to identify real user agents. Hand crafting your own agent could break at any time". 2 consequences I can think of: Google simply drops the traffic if it can't parse the User Agent OR marks it as bot/spider traffic (which will also be dropped if you have enabled the bot filtering option).

Although it's not mentioned in the documentation, I also suspect Google to rely on other data points, which could be:

I couldn't find more details on the topic, and I don't think you will find more details from Google explaining what they use to calculate browser/device because they don't want people messing with it (analogy: you won't find details about which data points are used for SEO, because they don't want people messing with it). The 4 dimensions I listed (User Agent, Screen resolution, Java Support, Flash version), are to my knowledge the only 4 that are device-specific from all GA collects (others are derived from them): https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&group=platform_or_device

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.

Related