Search code examples
javascriptgoogle-analyticsgoogle-analytics-apigoogle-analytics-firebasemeasurement-protocol

How to add code for Google Analytics custom dimensions for the same call/hit made prior, with some custom dimensions ( which take time to calculate )


I have GA embedded on my website. I know I can send some custom dimensions, using

var d1 = "valueForD1";
ga('set', 'dimension1', d1);
ga('send', 'pageview');

This is instantaneous.

Other way to do this by Measurement Protocol API (https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide).

with this

POST /collect HTTP/1.1
Host: www.google-analytics.com
payload_data

This hit is made(I make) when my server has completed some processing and calculated the custom dimension.

But I need to add only "dimension1" to the original GA data already got sent (in normal GA execution) without ga('set','dimension1',d1).

My point here is that this custom dimension takes some time to get calculated on server end. How will I add this dimension to the GA normal hit?


Solution

  • It depends. If this is a session- or user scoped dimension it should be enough to send a hit (e.g. via the measurement protocol) with the same client id (within the session lifetime, for a session scoped dimension). Session scope and user scope retain only the last value per session/user in any case, so it's enough to add it once per session.

    If this is a hit scoped dimension you are out of luck with a standard account. In a GA 360 account you can add dimensions and metrics to existing hits via query time import.