0
votes

Using the GA API v4, I'm trying to retrieve data from multiple segments, that have been defined inside Google Analytics. My application does not know the names of these segments, but let's say they are called A, B, C and D inside GA. (Other users can rename the segments, so the names are really unpredictable.)

My application does have the ID's of these segments hardcoded. (Manually retrieved from https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/segments/list.)

Now it's determined the list of data should be presented in some predefined order of significance. (That is, NOT in alphabetical order.)

So, I send a query to GA API with the segments array in the required order:

reportRequests["0"].segments[
  { segmentId: <id-for-segment-C> },
  { segmentId: <id-for-segment-B> },
  { segmentId: <id-for-segment-D> },
  { segmentId: <id-for-segment-A> }
]

To my surprise, the returned dataset is ordered in alphabetical order of segment name!

reports["0"].data.rows[
  { dimensions: [ "A" ], ... },
  { dimensions: [ "B" ], ... },
  { dimensions: [ "C" ], ... },
  { dimensions: [ "D" ], ... }
]

To make things worse:

  1. The returned data does not include the segment ID's, I can only correlate these by making a separate API call to retrieve information for which segment name corresponds with which ID.
  2. The alphabetic sorting is done with English rules, not with regional rules. (The segment names include the "Å" character, which according to Norwegian rules should be at the end of the alphabet, but is in the beginning for English sorting.)

Am I just imagining things? Am I causing problems by not completely understanding how the API works? Is this a bug in the API? Or is this actually by design?

1

1 Answers

0
votes

1) Are you using the v3 or v4 versions? If v3, give a try to v4 and see if it helps. An alternative would be to use dynamicSegments (which I always use) instead of segmentIds: these could be stored in your DB, solving the segment ID-name resolution problem, and would also avoid GA maintenance issues (whereby users modify segments which then breaks your app).

2) Yes, they probably transliterate (Å -> A) and sort only after. I really wouldn't waste time investigating this, I doubt Google will ever change it (it's not a bad behaviour per se, APIs are designed to support coding, not subtleties of the human language, most people, especially Google Engineers based in US, wouldn't care :))