1

I am tracking user events on the front end with google analytics, but I would also like to send back end events and be able to match up events for the same user in google analytics.

It looks like I should be able to pass the uid parameter: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid but it looks like I also have to pass the tid parameter https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tid .

The docs say that "All collected data is associated by this ID" (the tid).

What should I pass for the tid? Why can't I just pass the uid, if that is supposed to be a mechanism for tying events together?

I would like the backend to pass the uid to the front end (actually a one-way hash of the email), and then refer to the user in google analytics with this uid.

Is this feasible? I'm a bit confused about how to implement this.

Many thanks!

user384842
  • 1,946
  • 3
  • 17
  • 24

2 Answers2

0

The "tid" - Tracking ID - is the Web Property, i.e. the "slot" in your Analytics account that the data goes to. If you do not send a tracking id the calls will disappear in limbo. You find the tid in your property settings under "Tracking Code". It is a string that starts "UA-" and so is also sometimes referred to as UA-ID).

The User ID will not help you to identify users, at least not by default, since it is not exposed in the Analytics interface (it should really be called the "cross device identification id", since that is what it's for). You need to create a custom dimension and pass the value of the User ID there if you want to identify users. Per TOS you must take care that no third party, including Google, can resolve your User ID (or any other datapoint) into something that identifies a person, altough of course you can use yourself to connect data to other data in your backend system.

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
  • Thank you for your help. I see that I also need to supply a 'cid'. Since all events will get grouped by my uid anyway, does it matter what cid I pass from the backend? Does it have to be one that exists already or could I just use a random number say? – user384842 Jul 30 '15 at 15:40
  • There is a suggested format (UUID), but you can really use any number/string as long as you are sure it's unique. But if you mix requests from a front-end user and the backend (and they should go to the same user) you might get the cid from the Google Analytics cookie and use that (in that case you also need to provide the uip parameter with the user id). – Eike Pierstorff Jul 30 '15 at 15:50
0

Actually there is a proper way. I've implemented this for myself.

There's a Client ID parameter, that should be passed with your requests.

And here's you have two options:

  1. Create this client id manually (by generating UUID) on server-side and pass it to front-end. Then use this value when you create your tracker and also use it for server-side requests.

    //creating of a tracker with manually generated client id
    ga('create', 'UA-XXXXX-Y', {
      'storage': 'none',
      'clientId': '76c24efd-ec42-492a-92df-c62cfd4540a3'
    });
    

    Of course, you'll need to implement some logic of storing client id in cookie, for example.

  2. You can use client id that is being generated automatically by ga and then send it to the server-side by your method of choice. I've implemented it through cookies:

    // Creates a default tracker.
    ga('create', 'UA-XXXXX-Y', auto);
    
    // Gets the client ID of the default tracker and logs it.
    ga(function(tracker) {
      var clientId = tracker.get('clientId');
    
      //setting the cookie with jQuery help
      $.cookie("client-id", clientId , { path : "/" });
    });
    

    Then on the back-end just access this cookie and use that client id for your requests.

Also some information con be found here: What is the client ID when sending tracking data to google analytics via the measurement protocol?

Community
  • 1
  • 1
Gino Pane
  • 4,740
  • 5
  • 31
  • 46