We’ve migrated our documentation to a new site, which means some URLs have changed.
Audience

Audience: segments helper function - getSegments()

Summary

Overview

Respective segment IDs of the current user can be read with cX.getSegments() helper function. 

This function is simpler than the previous getUserSegmentIds() function. It became a universal function and supports User segments (Traffic, Lookalike, etc.), Contextual segments, and short IDs for these segment types. Additionally, it is possible to get regular segment IDs and short ID by the only request. 

It is recommended to use this function instead of the getUserSegmentIds(). 

Scope

The function provides: 

  • No caching of response values. 

  • Adding the current user ID to the request. 

Syntax

<span data-contrast="none">var segments = cX.getSegments(</span>persistedQueryId<span data-contrast="none">, callback);</span><span data-ccp-props='{"134233117":false,"134233118":false,"201341983":1,"335551550":0,"335551620":0,"335559685":-20,"335559737":-20,"335559738":420,"335559739":420,"335559740":420}'> </span>

Where: 

  • persistedQueryId. Required. The ID of the persisted query to execute. 

  • callback. Required. A function that will be called back with an array of segment IDs as the first argument. 

<span data-contrast="auto">var segments = cX.getSegment(persitedQueryId, function (segments) {})</span><span data-ccp-props='{"134233117":false,"134233118":false,"201341983":1,"335551550":0,"335551620":0,"335559685":-20,"335559737":-20,"335559738":105,"335559739":255,"335559740":420}'> </span>

Use cX.getSegments(persistedQueryId, callback, timeout) or cX.getUserSegmentIds({ persistedQueryId, callback, timeout }) to specify timeout in milliseconds.

Procedure

Create a persisted query

Create a persisted query that executes the wanted segment/lookup API request.
Now you can do this from the command line only, Admin UI does not support this. 

In the examples below, replace the "10431" siteGroupId with your actual site group ID!

/persisted/create example is below.

./cx.py '/persisted/create' '{
  "path": "/segment/lookup",
  "request": {
    "shortIds": true,
    "identity": { "id": "0", "type": "cx" },
    "siteGroupIds": ["10431"],
    "context": {
      "siteId": "0",
      "url": ""
    }
  },
  "description": "persisted for cX.getSegments with short ids",
  "mutable": { "identity": true, "context": true }
}'


If you have defined segments in two different site groups,  e.g. 10431 and 12332, the line "siteGroupIds" becomes: "siteGroupIds": ["10431", "12332"]

If you want to use short IDs, you should pass request: { shortIds: true} to persisted/create; both options oblige to specify "mutable": { "identity": true, "context": true } when creating a persisted query.

This command returns the identity of the newly created persisted query, something like this: 

927c1bd9548fb2879079c3837d6169a3892b21af

Copy the persisted query ID to a usage example

Copy the persisted query ID to one of the usage examples below.

var defaultSegments = [
  { id: "foo1", type: "traffic" },
  { id: "foo2", type: "traffic" },
];

cX.getSegments("persistedQueryId", function (segments) {
  console.log(segments);                      // 1. output cxTyped segments

  var segments = segments || defaultSegments; // 2. use defaultSegments if needed

  var ids = [];
  var onlyContextualIds = [];

  cX.Array.forEach(segments, function (cxTypedSegment, index) {
    if (cxTypedSegment.type === "contextual") {
      onlyContextualIds.push(cxTypedSegment.id);
    } else {
      ids.push(cxTypedSegment.id);
    }
  });

  console.log(onlyContextualIds);              // 3. output only contextual segments id
  console.log(ids);                            // 4. output other types segments id

  googletag.setTargeting("CxSegments", ids);
  googletag.setTargeting("CxContext", onlyContextualShortIds);

  syncWithAnotherAdServer2(shortIds);
});

cX.getSegments('persistedQueryIdWithPredefinedShortIdParam', function(segments) {
    var shortIds = [];
    cX.Array.forEach(segments, function (cxTypedSegment, index) {
        shortIds.push(cxTypedSegment.shortId);
    });
    console.log(shortIds);
    googletag.setTargeting('CxSegments', shortIds);
});

Last updated: