{"_id":"5a6b684793313c005b692e1d","category":{"_id":"5a6b684793313c005b692e0a","version":"5a6b684793313c005b692e07","project":"57c87ef85754fa1700b1228d","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-11-05T21:53:37.386Z","from_sync":false,"order":2,"slug":"reference","title":"Reference"},"parentDoc":null,"project":"57c87ef85754fa1700b1228d","user":"55e5ba046015ce1900eadb8e","version":{"_id":"5a6b684793313c005b692e07","project":"57c87ef85754fa1700b1228d","__v":1,"createdAt":"2018-01-26T17:41:27.297Z","releaseDate":"2018-01-26T17:41:27.297Z","categories":["5a6b684793313c005b692e08","5a6b684793313c005b692e09","5a6b684793313c005b692e0a","5a6b684793313c005b692e0b"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"3.5.3","version":"3.5.3"},"__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-03-21T09:58:29.999Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":12,"body":"The SDK allows you to manage your user's state, properties, permissions, and purchases with ease. It's important to note that Primer does not provide a user storage system; you should still implement that functionality yourself.\n\n\n\n####State Management\n\nThe Primer SDK automatically manages the user's state when it comes to signing up and logging in through the Primer experience's Signup or Login flows. However there might be other situations when your app allows users to sign up or log in. In these cases, you should notify the SDK about user state changes to appropriately track their behaviour and manage their properties.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Sign a user up manually\\nPrimer.signupUser(\\\"user1234\\\");\\n\\n// Log a user in manually\\nPrimer.loginUser(\\\"user1234\\\");\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nIf your app supports logging out there's also a way to let the SDK know about the change. When you call this, Primer resets all state information, user properties, and Facebook sessions.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Primer.logout();\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n####Current User\n\nEven if a user did not sign up or log in yet you can access a lot of information about them. To do so just get the current `PrimerUser` instance and then access it's properties.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"PrimerUser user = Primer.getCurrentUser();\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nThe available properties are:\n* the unique user identifier,\n* the name of the user,\n* the user properties attached,\n* the user's active A/B/N tests,\n* the last viewed variation.\n\nNote that some of this information is only available after certain events. For example the user ID is not set until the user signs up or logs in; the name is only available once you set it; and the last viewed variation becomes available after the first Primer experience gets presented.\n\n####User Properties\n\nThe SDK keeps track of properties attached to each user, and syncs this information with the Primer servers, so whenever you log a user in with the same unique user ID, the same properties you attached earlier will be present. User properties are also attached to all events that the current user triggers, and they can be used for dynamic variable replacements on Primer experience screens. If you'd like to append some properties to the current user, just pass in a dictionary filled with the keys and values.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"HashMap<String, String> userProperties = new HashMap<>();\\nuserProperties.put(\\\"name\\\", \\\"Steve\\\");\\nuserProperties.put(\\\"gender\\\", \\\"male\\\");\\nuserProperties.put(\\\"age\\\", \\\"42\\\");\\n\\nPrimer.appendUserProperties(userProperties);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"The `profile_pic` key is often used to personalize Primer experiences, so if you set it to anything other than an image or an image URL, some screens might look broken.\"\n}\n[/block]","excerpt":"","slug":"user-management","type":"basic","title":"User State Management"}

User State Management


The SDK allows you to manage your user's state, properties, permissions, and purchases with ease. It's important to note that Primer does not provide a user storage system; you should still implement that functionality yourself. ####State Management The Primer SDK automatically manages the user's state when it comes to signing up and logging in through the Primer experience's Signup or Login flows. However there might be other situations when your app allows users to sign up or log in. In these cases, you should notify the SDK about user state changes to appropriately track their behaviour and manage their properties. [block:code] { "codes": [ { "code": "// Sign a user up manually\nPrimer.signupUser(\"user1234\");\n\n// Log a user in manually\nPrimer.loginUser(\"user1234\");", "language": "java" } ] } [/block] If your app supports logging out there's also a way to let the SDK know about the change. When you call this, Primer resets all state information, user properties, and Facebook sessions. [block:code] { "codes": [ { "code": "Primer.logout();", "language": "java" } ] } [/block] ####Current User Even if a user did not sign up or log in yet you can access a lot of information about them. To do so just get the current `PrimerUser` instance and then access it's properties. [block:code] { "codes": [ { "code": "PrimerUser user = Primer.getCurrentUser();", "language": "java" } ] } [/block] The available properties are: * the unique user identifier, * the name of the user, * the user properties attached, * the user's active A/B/N tests, * the last viewed variation. Note that some of this information is only available after certain events. For example the user ID is not set until the user signs up or logs in; the name is only available once you set it; and the last viewed variation becomes available after the first Primer experience gets presented. ####User Properties The SDK keeps track of properties attached to each user, and syncs this information with the Primer servers, so whenever you log a user in with the same unique user ID, the same properties you attached earlier will be present. User properties are also attached to all events that the current user triggers, and they can be used for dynamic variable replacements on Primer experience screens. If you'd like to append some properties to the current user, just pass in a dictionary filled with the keys and values. [block:code] { "codes": [ { "code": "HashMap<String, String> userProperties = new HashMap<>();\nuserProperties.put(\"name\", \"Steve\");\nuserProperties.put(\"gender\", \"male\");\nuserProperties.put(\"age\", \"42\");\n\nPrimer.appendUserProperties(userProperties);", "language": "java" } ] } [/block] [block:callout] { "type": "info", "body": "The `profile_pic` key is often used to personalize Primer experiences, so if you set it to anything other than an image or an image URL, some screens might look broken." } [/block]