1

I'm sending some events from Python backend to Google Analytics via requests library using Measurement Protocol. The code is straightforward:

GA_ID = settings.GOOGLE_ANALYTICS_PROPERTY_ID

class GoogleAnalytics:

    def __init__(self):
        self._host = 'https://www.google-analytics.com/collect'
        self._v = '1'
        self._tid = GA_ID
        self._cid = str(uuid.uuid4())

    def send_event(self, ec, ea, el):
        payload = {
            'v': self._v,
            'tid': self._tid,
            'cid': self._cid,
            't': 'event',
            'ec': ec,
            'ea': ea,
            'el': el
        }

        r = requests.post(url=self._host, data=payload)

However, when event is sent to GA, it is also reflected as an user visit in Audience with User-agent requests 2.18.4 - this is undesirable since it has nothing to do with actual user. Is there any way to prevent such behavior?

UPD: I am not looking for an option to change User-Agent header. I am looking for an option to exclude GAMP event hits registered as user visits.

paulus
  • 655
  • 7
  • 22
  • [sending-user-agent-using-requests-library-in-python](https://stackoverflow.com/questions/10606133/sending-user-agent-using-requests-library-in-python) – t.m.adam Feb 11 '18 at 11:01
  • What I am looking for is the solution to completely avoid registering GAMP event hits as user vists. Simply changing User-Agent header to a 'friendlier' name I guess won't help. – paulus Feb 11 '18 at 11:10

1 Answers1

1

You can't omit measurement protocol hits from user calculations because the concept of users is core to GA's reporting architecture.

A workaround, however, is to mimic all your measurement protocol hits as belonging to a single user. You can do this by passing a fixed value for your cid or uid parameters. This method will not inflate your user numbers.

vinoaj
  • 1,600
  • 10
  • 8