Adding Pinpoint Analytics to an existing project

Credit: Amazon AWS Documentation

If you are already using AWS Cognito for authentication and you decided to use their analytics tool, Pinpoint, you might get frustrated after hours of reading tutorials and QA on their github or Stackoverflow, as I did. There are a few resources with titles such as “Use existing AWS resources”, like this and this, you try them, no luck.

What’s wrong? maybe your config json is not correct? Maybe try generating the amplifyconfiguration.json from an empty project and use it in yours, sounds good, but it won’t work either. If you are lucky (using the right json file) you can figure out what’s going on by enabling debugging for Amplify SDK in your client.

Long story short, I did all of the above steps, almost two days to find out why it’s not working.

First, do create an empty project with amplify, follow their document of course, and get your amplifyconfiguration.json file. You will get something like this:

{
    "UserAgent": "aws-amplify-cli/2.0",
    "Version": "1.0",
    "analytics": {
        "plugins": {
            "awsPinpointAnalyticsPlugin": {
                "pinpointAnalytics": {
                    "appId": "[APP_ID]",
                    "region": "[REGION]"
                },
                "pinpointTargeting": {
                    "region": "[REGION]"
                }
            }
        }
    }, 
   "auth": {
        "plugins": {
            "awsCognitoAuthPlugin": {
                "UserAgent": "aws-amplify-cli/0.1.0",
                "Version": "0.1.0",
                "IdentityManager": {
                    "Default": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "[IDENTITY_POOL_ID]",
                            "Region": "[REGION]"
                        }
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH"
                    }
                },
                "PinpointAnalytics": {
                    "Default": {
                        "AppId": "[APP_ID]",
                        "Region": "[REGION]"
                    }
                },
                "PinpointTargeting": {
                    "Default": {
                        "Region": "[REGION]"
                    }
                }
            }
        }
    }
}

As you can see, you need an Identity Pool even if you are not using it, the SDK needs it. The other thing which is not mentioned in the documents is you need to add their auth plugin because the analytics plugin requires it. So on your client side you need to configure the Amplify SDK with auth and analytics plugin.

Everything good? nah, not yet. If you run your app you will see in the debugging logs that there are some permission issues, now you need to configure a permissions on your AWS dashboard which I am not familiar with and I didn’t do this part, but all I know is the identity pool needs two grants, at least in our case it was two, mobileanalytics and mobiletargeting:

Now, you are good to go!

PS: you might need some more configs on your AWS dashboard which is beyond my knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.