Search code examples
amazon-web-servicesamazon-cognitoaws-pinpoint

How do you connect Cognito to Pinpoint for analytics?


I'm trying to get some basic analytics for a Cognito user pool. It seemed simple enough to do, I created a project in Pinpoint, then I went to my user pool, went to General settings > Analytics, clicked the Add Analytics button, choose the Amazon Cognito app client that my app uses, pointed to the Pinpoint project I just created, checked the "Share user profile data" checkbox (though I assume that's not absolutely necessary), and it had the message telling me if would use the IAM role and so on. Clicked Save, got no error, I assumed at this point I would start seeing Analytics in Pinpoint, but there's absolutely nothing showing up.I do have a message saying I haven't enabled any features yet, but I don't see any features I'd need to enable. I don't care about the campaigns as of now, and then under Application analytics it seems geared to you manually updating your mobile or web app to send something, but I thought that was if you need to customize something. Am I mistaken? Will this integration only work if you change your web app to explicitly send things to Pinpoint? I just assumed if I connected Cognito and Pinpoint the analytics would show since Cognito obviously knows people are logging in without you needing to manually make some extra request.


Solution

  • From my research, I found out that since you are using a Web Application without using AWS Amplify framework, you need to add additional lines of code to your application in order to send Cognito authentication analytics data to your Amazon Pinpoint project.

    1. If you are using the Javascript SDK you may add the initate-Auth property code snippet to your front-end application:

       var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
      
       var params = {
         AuthFlow: "USER_PASSWORD_AUTH",
         ClientId: 'STRING_VALUE', /* the client ID attached to the Pinpoint project */
         AuthParameters: {
           'USERNAME': 'STRING_VALUE',
           'PASSWORD': 'STRING_VALUE'
         },
         AnalyticsMetadata: {
           AnalyticsEndpointId: 'STRING_VALUE' /* the Pinpoint project ID */
         },
       };
       cognitoidentityserviceprovider.initiateAuth(params, function(err, data) {
         if (err) console.log(err, err.stack); // an error occurred
         else     console.log(data);           // successful response
       });
      
    • In the above code snippet, the Pinpoint project/application ID is added as part of the "AnalyticsMetadata" parameter when the client makes the API calls (e.g. sign-in, sign-up, etc.) to the Cognito user pool. Therefore, the API calls will have the pinpoint project ID attached to them and Cognito can use that information to send the data to your Pinpoint project. Without this crucial step, analytics data will not be sent to Pinpoint and will result in the behavior you have described.
    1. If using CLI (for verification/testing purpose), you may execute the following AWS CLI initiate-auth command below :

      $ aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --auth-parameters USERNAME=STRING_VALUE,PASSWORD=STRING_VALUE --client-id STRING_VALUE --analytics-metadata AnalyticsEndpointId=STRING_VALUE
      

    The take away point is that :

    Amazon Cognito integration with Amazon Pinpoint only works for apps (clients) that are using Cognito SDK for Mobile Apps (AWS Mobile SDKs or JavaScript - AWS Amplify JavaScript library).