{"id":21478309,"url":"https://github.com/appsflyersdk/appsflyer-native-pc-unity-sample-app","last_synced_at":"2025-07-24T13:41:11.711Z","repository":{"id":177807036,"uuid":"650570718","full_name":"AppsFlyerSDK/appsflyer-native-pc-unity-sample-app","owner":"AppsFlyerSDK","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-16T15:06:06.000Z","size":107,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-23T03:41:07.064Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AppsFlyerSDK.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-07T10:53:37.000Z","updated_at":"2023-07-01T08:41:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"78ea42c8-5515-44cf-a0a8-35405fdde3de","html_url":"https://github.com/AppsFlyerSDK/appsflyer-native-pc-unity-sample-app","commit_stats":null,"previous_names":["appsflyersdk/appsflyer-native-pc-unity-sample-app"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AppsFlyerSDK%2Fappsflyer-native-pc-unity-sample-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AppsFlyerSDK%2Fappsflyer-native-pc-unity-sample-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AppsFlyerSDK%2Fappsflyer-native-pc-unity-sample-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AppsFlyerSDK%2Fappsflyer-native-pc-unity-sample-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AppsFlyerSDK","download_url":"https://codeload.github.com/AppsFlyerSDK/appsflyer-native-pc-unity-sample-app/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243997164,"owners_count":20380982,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T11:17:38.787Z","updated_at":"2025-03-17T08:24:01.330Z","avatar_url":"https://github.com/AppsFlyerSDK.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: Native PC Unity\nparentDoc: 64ad6456ceede10cf0b2a120\ncategory: 6446526dddf659006c7ea807\norder: 1\nhidden: false\nslug: unity-nativepc\n---\n\n\u003e Link to repository  \n\u003e [GitHub](https://github.com/AppsFlyerSDK/appsflyer-native-pc-unity-sample-app)\n\n## AppsFlyer Native PC Unity SDK integration\n\nAppsFlyer empowers gaming marketers to make better decisions by providing powerful tools to perform cross-platform attribution.\n\nGame attribution requires the game to integrate the AppsFlyer SDK that records first opens, consecutive sessions, and in-app events. For example, purchase events.\nWe recommend you use this sample app as a reference for integrating the AppsFlyer SDK into your Unity Native PC game.\n\n**Note**: The sample code that follows is supported in a both Windows \u0026 Mac environment.\n\n\u003chr/\u003e\n\n## AppsflyerModule - Interface\n\n`AppsflyerModule.cs`, included in the scenes folder, contains the required code and logic to connect to AppsFlyer servers and report events.\n\n### AppsflyerModule\n\nThis method receives your API key, App ID, the parent MonoBehaviour and a sandbox mode flag (optional, false by default) and initializes the AppsFlyer Module.\n\n**Method signature**\n\n```c#\nAppsflyerModule(string devkey, string appid, MonoBehaviour mono, bool isSandbox = false)\n```\n\n**Arguments**:\n\n- `DEV_KEY`: Get from the marketer or [AppsFlyer HQ](https://support.appsflyer.com/hc/en-us/articles/211719806-App-settings-#general-app-settings).\n- `APP_ID`: The app id on Appsflyer HQ (excluding the `nativepc-` prefix).\n- `MonoBehaviour mono`: the parent MonoBehaviour.\n- `bool isSandbox`: Whether to activate sandbox mode. False by default. This option is for debugging. With the sandbox mode, AppsFlyer dashboard does not show the data. \n\n**Usage**:\n\n```c#\n// for regular init\nAppsflyerModule afm = new AppsflyerModule(\u003c\u003c DEV_KEY \u003e\u003e, \u003c\u003c APP_ID \u003e\u003e, this);\n\n// for init in sandbox mode (reports the events to the sandbox endpoint)\nAppsflyerModule afm = new AppsflyerModule(\u003c\u003c DEV_KEY \u003e\u003e, \u003c\u003c APP_ID \u003e\u003e, this, true);\n```\n\n### Start\n\nThis method sends first open/session requests to AppsFlyer.\n\n**Method signature**\n\n```c#\nvoid Start(bool skipFirst = false)\n```\n\n**Arguments**\n\n- `bool skipFirst`: Determines whether or not to skip first open events and send session events. The value is false by default. If true , first open events are skipped and session events are sent. [See example](#skipFirstExample)\n\n\n**Usage**:\n\n```c#\n// without the flag\nafm.Start();\n\n// with the flag\nbool skipFirst = [SOME_CONDITION];\nafm.Start(skipFirst);\n```\n\n\n### Stop\n\nThis method stops the SDK from functioning and communicating with AppsFlyer servers. It's used when implementing user opt-in/opt-out.\n\n**Method signature**\n\n```c#\nvoid Stop()\n```\n\n**Usage**:\n\n```c#\n// Starting the SDK\nafm.Start();\n// ...\n// Stopping the SDK, preventing further communication with AppsFlyer\nafm.Stop();\n```\n\n### LogEvent\n\nThis method receives an event name and JSON object and sends an in-app event to AppsFlyer.\n\n**Method signature**\n\n```c#\nvoid LogEvent(\n      string event_name,\n      Dictionary\u003cstring, object\u003e event_parameters,\n      Dictionary\u003cstring, object\u003e event_custom_parameters = null\n   )\n```\n\n**Arguments**:\n\n- `string event_name`: the name of the event.\n- `Dictionary\u003cstring, object\u003e event_parameters`: dictionary object which contains the [predefined event parameters](https://dev.appsflyer.com/hc/docs/ctv-log-event-event-parameters).\n- `Dictionary\u003cstring, object\u003e event_custom_parameters`: (non-mandatory): dictionary object which contains the any custom event parameters.\n\n**Usage**:\n\n```c#\n// set event name\nstring event_name = \"af_purchase\";\n// set event values\nDictionary\u003cstring, object\u003e event_parameters = new Dictionary\u003cstring, object\u003e();\nevent_parameters.Add(\"af_currency\", \"USD\");\nevent_parameters.Add(\"af_revenue\", 12.12);\n// send logEvent request\nafm.LogEvent(event_name, event_parameters);\n\n// send logEvent request with custom params\nDictionary\u003cstring, object\u003e event_custom_parameters = new Dictionary\u003cstring, object\u003e();\nevent_custom_parameters.Add(\"goodsName\", \"新人邀约购物日\");\nafm.LogEvent(event_name, event_parameters, event_custom_parameters);\n```\n\n### IsInstallOlderThanDate\n\nThis method receives a date string and returns true if the game folder creation date is older than the date string. The date string format is: \"2023-03-01T23:12:34+00:00\"\n\n**Method signature**\n\n```c#\nbool IsInstallOlderThanDate(string datestring)\n```\n\n**Arguments**:\n\n- `string datestring`: Date string in `yyyy-mm-ddThh:mm:ss+hh:mm` format.\n\n\n**Usage**:\n\u003cdiv id=\"skipFirstExample\"\u003e\u003c/div\u003e\n\n```c#\n// the creation date in this example is \"2023-03-23T08:30:00+00:00\"\nbool newerDate = afm.IsInstallOlderThanDate(\"2023-06-13T10:00:00+00:00\");\nbool olderDate = afm.IsInstallOlderThanDate(\"2023-02-11T10:00:00+00:00\");\n\n// will return true\nDebug.Log(\"newerDate:\" + (newerDate ? \"true\" : \"false\"));\n// will return false\nDebug.Log(\"olderDate:\" + (olderDate ? \"true\" : \"false\"));\n\n// example usage with skipFirst -\n// skipping if the install date is NOT older than the given date\nbool IsInstallOlderThanDate = afm.IsInstallOlderThanDate(\"2023-02-11T10:00:00+00:00\");\nafm.Start(!IsInstallOlderThanDate);\n```\n\n### SetCustomerUserId\n\nThis method sets a customer ID that enables you to cross-reference your unique ID with the AppsFlyer unique ID and other device IDs. Note: You can only use this method before calling `Start()`.\nThe customer ID is available in raw data reports and in the postbacks sent via API.\n\n**Method signature**\n\n```c#\nvoid SetCustomerUserId(string cuid)\n```\n\n**Arguments**:\n\n- `string cuid`: Custom user id.\n\n**Usage**:\n\n```c#\nAppsflyerSteamModule afm = new AppsflyerSteamModule(DEV_KEY, STEAM_APP_ID, this);\nafm.SetCustomerUserId(\"15667737-366d-4994-ac8b-653fe6b2be4a\");\nafm.Start();\n```\n\n### SetSharingFilterForPartners\n\nThis method lets you configure which partners should the SDK exclude from data-sharing. Partners that are excluded with this method will not receive data through postbacks, APIs, raw data reports, or any other means.\n\n**Method signature**\n\n```c#\npublic void SetSharingFilterForPartners(List\u003cstring\u003e sharingFilter)\n```\n\n**Arguments**:\n\n- `List\u003cstring\u003e sharingFilter`: a list of partners to filter. For example:  `new List\u003cstring\u003e() {\"partner1_int\", \"partner2_int\"};`\n\n**Usage**:\n\n```c#\nAppsflyerModule afm = new AppsflyerModule(DEV_KEY, APP_ID, this);\n\n// set the sharing filter\nvar sharingFilter = new List\u003cstring\u003e() {\"partner1_int\", \"partner2_int\"};\nafm.SetSharingFilterForPartners(sharingFilter);\n\n// start the SDK (send firstopen/session request)\nafm.Start();\n```\n\n### GetAppsFlyerUID\n\nGet AppsFlyer's unique device ID. The SDK generates an AppsFlyer unique device ID upon app installation. When the SDK is started, this ID is recorded as the ID of the first app install.\n\n**Method signature**\n\n```c#\nvoid GetAppsFlyerUID()\n```\n\n**Usage**:\n\n```c#\nAppsflyerModule afm = new AppsflyerModule(\u003c\u003c DEV_KEY \u003e\u003e, \u003c\u003c APP_ID \u003e\u003e, this);\nafm.Start();\nstring af_uid = afm.GetAppsFlyerUID();\n```\n\n## Running the sample app\n\n1. Open Unity hub and open the project.\n2. Use the sample code in AppsflyerScript.cs and update it with your DEV_KEY and APP_ID.\n3. Add the AppsflyerScript to an empty game object (or use the one in the scenes folder):  \n   ![Request-OK](https://files.readme.io/b271553-small-EpicGameObject.PNG)\n4. Launch the sample app via the Unity editor and check that your debug log shows the following message:  \n   ![Request-OK](https://files.readme.io/7105a10-small-202OK.PNG)\n5. After 24 hours, the AppsFlyer dashboard updates and shows organic and non-organic installs and in-app events.\n\n## Implementing AppsFlyer in your Native PC game\n\n### Setup\n\n1. Add the script from `Assets/AppsflyerModule.cs` to your app.\n2. Use the sample code in `Assets/AppsflyerScript.cs` and update it with your `DEV_KEY` and `APP_ID`.\n3. Initialize the SDK.\n\n```c#\nAppsflyerModule afm = new AppsflyerModule(\u003c\u003c DEV_KEY \u003e\u003e, \u003c\u003c APP_ID \u003e\u003e, this);\n```\n\n6. [Start](#start) the AppsFlyer integration.\n7. Report [in-app events](#logevent).\n\n## Resetting the attribution\n\n[Delete the PlayerPrefs data the registry/preferences folder](https://docs.unity3d.com/ScriptReference/PlayerPrefs.html), or use [PlayerPrefs.DeleteAll()](https://docs.unity3d.com/2020.1/Documentation/ScriptReference/PlayerPrefs.DeleteAll.html) when testing the attribution in the UnityEditor.\n![AF guid \u0026 counter in the Windows Registry](https://files.readme.io/51b1681-image.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappsflyersdk%2Fappsflyer-native-pc-unity-sample-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fappsflyersdk%2Fappsflyer-native-pc-unity-sample-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fappsflyersdk%2Fappsflyer-native-pc-unity-sample-app/lists"}