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

Audience Integrations: Google

Integrating Piano Audience with Google allows you to link a Piano user ID with a Google user ID which you can then use for audience segments, reporting and targeting.


  1. For you to get the current user's Google user ID, the user has to be logged in to Google on the current page. This only needs to happen once (more about Google Sign-in).

  2. Create a prefix to link the Google ID to, e.g. ggl(more about Customer Prefix).

  3. Create a persisted query for /profile/user/external/link/update, where you lock down the type to be your prefix and make id and cxid mutable (more about Persisted Queries.)

  4. (Optionally) The Google user ID can be one-way hashed to prevent subsequent getting a Google user from a Piano user ID.

  5. Add this tag anywhere on the page (preferably earlier in the file to make sure that it is loaded before the user navigates somewhere else):

<!-- Piano Audience Google user linking script begin -->
<script type="text/javascript">
    window.cX = window.cX || {}; cX.callQueue = cX.callQueue || [];
    cX.callQueue.push(['invoke', function() {
        var intervalId = setInterval(function() {
            if (window.gapi && gapi.auth2 && typeof gapi.auth2.getAuthInstance === 'function') {
                var auth = gapi.auth2.getAuthInstance();
                if (auth && auth.isSignedIn && typeof auth.isSignedIn.get === 'function' && auth.isSignedIn.get()) {
                    clearInterval(intervalId);
 
                    // Read the Google user ID
                    var googleUser = auth.currentUser.get();
                    var googleUserId = googleUser.getBasicProfile().getId();
 
                    // Map the Google user ID to the Cxense user ID
                    var persistedQueryId = '<PERSISTED-QUERY-ID>'; // <-- TODO: Insert persisted query id here
                    var apiUrl = 'https://api.cxense.com/profile/user/external/link/update?callback={{callback}}'
                        + '&persisted=' + encodeURIComponent(persistedQueryId)
                        + '&json=' + encodeURIComponent(cX.JSON.stringify({ id: googleUserId, cxid: cX.getUserId() }));
 
                    console.log('Linking: ' + JSON.stringify({ id: googleUserId, cxid: cX.getUserId() }));
                    cX.jsonpRequest(apiUrl, function (data) { /* If wanted, verify success here*/ });
                }
            }
        }, 1000);
    }]);
    // Async load cx.js
    (function(d,s,e,t){e=d.createElement(s);e.type='text/java'+s;e.async='async';
        e.src='http'+('https:'===location.protocol?'s://s':'://')+'cdn.cxense.com/cx.js';
        t=d.getElementsByTagName(s)[0];t.parentNode.insertBefore(e,t);})(document,'script');
</script>
<!-- Piano Audience Google user linking script end -->

Last updated: