Windows C-Sharp Advanced Features

Initialize Branch

Branch.I.InitSession(new BranchInitCallbackWrapper((buo, linkProperties, error) => { }));
Branch.I.InitSession(new BranchInitCallbackWrapper((parameters, error) => { }));

Retrieve Install Parameters

If you ever want to access the original session params (the parameters passed in for the first install event only), you can use this line. This is useful if you only want to reward users who newly installed the app from a referral link or something.

var installParams = Branch.I.GetFirstParams();

Persistent Identities

Often, you might have the own user IDs, or want referral and event data to persist across platforms or uninstall/reinstall. It's helpful if you know the users access the service from different devices. This where we introduce the concept of an 'identity'.

To identify a user, just call:

Branch.I.SetIdentity("User1488", (referringParams, error) => { //callback });

Logout

If you provide a logout function in the app, be sure to clear the user when the logout completes. This will ensure that all the stored parameters get cleared and all events are properly attributed to the right identity.

Warning this call will clear the referral credits and attribution on the device.

Branch.I.Logout((logout, error) => { //callback });

Tracking User Actions and Events

Use BranchEvent class to track special user actions or application specific events beyond app installs, opens, and sharing. You can track events such as when a user adds an item to an on-line shopping cart, or searches for a keyword etc. BranchEvent provides an interface to add content(s) represented by a BranchUniversalObject in order to associate content(s) with events. You can view analytics for the BranchEvents you fire on the Branch dashboard. BranchEventType enumerate the most commonly tracked events and event parameters that can be used with BranchEvent for the best results. You can always use custom event names and event parameters.

BranchEvent ev = new BranchEvent("test_custom_events")
    .SetDescription("Test description")
    .SetTransactionID("322")
    .AddCustomDataProperty("TestProperty", "TestValue");
ev.LogEvent();

Generating Branch Links

Branch links can be created in-app as well as in many other ways. When they are, and setIdentity has been called to associate a User ID with the current user session, Branch links will be associated with that User ID.

// you can access this data from any instance that installs or opens the app from this link

BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
   .SetCanonicalIdentifier("item/12345")
   .SetCanonicalUrl("https://branch.io/deepviews")
   .SetContentIndexingMode(BranchUniversalObject.ContentIndexModes.PRIVATE)
   .SetLocalIndexMode(BranchUniversalObject.ContentIndexModes.PUBLIC)
   .SetTitle("My Content Title")
   .SetContentDescription("my_product_description1")
   .SetContentImageUrl("https://example.com/mycontent-12345.png")
   .SetContentExpiration(DateTime.UtcNow)
   .SetContentImageUrl("https://test_img_url")
   .AddKeyWord("My_Keyword1")
   .AddKeyWord("My_Keyword2")
   .SetContentMetadata(
        new BranchContentMetadata().AddCustomMetadata("testkey", "testvalue")
   );

BranchLinkProperties linkProperties = new BranchLinkProperties()
         .AddTag("Tag1")
         .SetChannel("Sharing_Channel_name")
         .SetFeature("my_feature_name")
         .AddControlParameter("$android_deeplink_path", "custom/path/*")
         .AddControlParameter("$ios_url", "http://example.com/ios")
         .SetDuration(100);

string url = branchUniversalObject.GetShortURL(linkProperties);

Note
You can customize the Facebook OG tags of each URL if you want to dynamically share content by using the following optional keys in the data dictionary. Please use this Facebook tool to debug the OG tags!

KeyValue
$og_titleThe title you'd like to appear for the link in social media
$og_descriptionThe description you'd like to appear for the link in social media
$og_image_urlThe URL for the image you'd like to appear for the link in social media
$og_videoThe URL for the video
$og_urlThe URL you'd like to appear
$og_app_idthe OG app ID. Optional and rarely used.

Also, you do custom redirection by inserting the following optional keys in the dictionary:

KeyValue
$desktop_urlWhere to send the user on a desktop or laptop. By default it is the Branch-hosted text-me service
$android_urlThe replacement URL for the Play Store to send the user if they don't have the app. Only necessary if you want a mobile web splash
$ios_urlThe replacement URL for the App Store to send the user if they don't have the app. Only necessary if you want a mobile web splash
$ipad_urlSame as above but for iPad Store
$fire_urlSame as above but for Amazon Fire Store
$blackberry_urlSame as above but for Blackberry Store
$windows_phone_urlSame as above but for Windows Store

You have the ability to control the direct deep linking of each link by inserting the following optional keys in the dictionary:

KeyValue
"$deeplink_pathThe value of the deep link path that you'd like us to append to the URI. For example, you could specify "$deeplink_path": "radio/station/456" and we'll open the app with the URI "theapp://radio/station/456?link_click_id=branch-identifier". This is primarily for supporting legacy deep linking infrastructure.
"$always_deeplinktrue or false. (default is not to deep link first) This key can be specified to have our linking service force try to open the app, even if we're not sure the user has the app installed. If the app is not installed, we fall back to the respective app store or $platform_url key. By default, we only open the app if we've seen a user initiate a session in the app from a Branch link (has been cookied and deep linked by Branch)

Link sharing

You can share your generated link with that method:

BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
                   .SetCanonicalIdentifier("item/12345")
                   .SetCanonicalUrl("https://branch.io/deepviews")
                   .SetContentIndexingMode(BranchUniversalObject.ContentIndexModes.PRIVATE)
                   .SetLocalIndexMode(BranchUniversalObject.ContentIndexModes.PUBLIC)
                   .SetTitle("My Content Title")
                   .SetContentDescription("my_product_description1")
                   .SetContentImageUrl("https://example.com/mycontent-12345.png")
                   .SetContentExpiration(DateTime.UtcNow)
                   .SetContentImageUrl("https://test_img_url")
                   .AddKeyWord("My_Keyword1")
                   .AddKeyWord("My_Keyword2")
                   .SetContentMetadata(
                        new BranchContentMetadata().AddCustomMetadata("testkey", "testvalue")
                   );

            BranchLinkProperties linkProperties = new BranchLinkProperties()
                     .AddTag("Tag1")
                     .SetChannel("Sharing_Channel_name")
                     .SetFeature("my_feature_name")
                     .AddControlParameter("$android_deeplink_path", "custom/path/*")
                     .AddControlParameter("$ios_url", "http://example.com/ios")
                     .SetDuration(100);

            BranchShareSheetStyle style = new BranchShareSheetStyle("Test share title", "Test share message body");
            style.SetDefaultUrl("https://branch.io/");
            branchUniversalObject.ShowShareSheet(DataTransferManager.GetForCurrentView(), Dispatcher, linkProperties, style);

BranchShareSheetStyle
This class is need for customize "Link sharing"

  • Its constructor takes two arguments title and message
  • Method SetDefaultUrl allows you set default url if url generating was failed
  • Method SetBitmap set image for your share

❗️

DataTransferManager.GetForCurrentView() and Dispatcher should be called from main thread

Branch Metrics Windows COM

Creating the Project

  1. Start Microsoft Visual Studio
  2. On the File menu, point to
    New, and then click Project. The New Project dialog box opens.
  3. Under Installed, click Visual C++.
  4. Click Windows Console Application.

Setup COM Branch Library

  1. Build solution
  2. Copy COM Library from
    ..\BranchWindowsSdk\DLLs\Debug\branch_debug_0.1.0.tlb or
    ..\BranchWindowsSdk\DLLs\Release\branch_0.1.0.tlb to
    ..\Your Project Folder\Your Project Name\Debug or
    ..\Your Project Folder\Your Project Name\Release
  3. Import library
#import "..\Your Project Name\Debug\branch_debug_0.1.0.tlb"
using namespace branch_debug_0.1.0;
or
#import "..\Your Project Name\Debug\branch_0.1.0.tlb"
using namespace branch_0.1.0;
  1. Include <thread>
#include <thread>

Initialize COM

To initialize the use of a COM, paste this code into your main class.

int main()
{
    HRESULT hr = CoInitialize(NULL);
    //In next your code will be here
    CoUninitialize();
}

COM Branch Class

To create COM Branch class use this:

ICOMBranchPtr comBranch(__uuidof(COMBranch));
Next call ``GetBranchInstance`` and ``SetNetworkTimeout()`` methods
comBranch->GetBranchInstance(false, "your branch key");
comBranch->SetNetworkTimeout(3000);
COM Branch Methods:

 - void GetBranchInstance(bool isLive, string key);
 - void EnableTestMode();
 - void SetDebug(bool isDebug);
 - void DisableTracking(bool disableTracking);
 - bool IsTrackingDisabled();
 - void SetNetworkTimeout(int timeout);
 - void SetMaxRetries(maxRetries);
 - void SetRetryInterval(int retryInterval);
 - void SetRequestMetadata(string key, string value);
 - void EnableSimulateInstall();
 - void DisableSimulateInstall();
 - string GetFirstParams();
 - string GetSessionParams();
 - void SetIdentity(string userID, void* callback);
 - void Logout(void* callback);
 - void RedeemRewards(int count);
 - void RedeemRewards(int count, void* callback);
 - void RedeemRewards(string bucket, int count);
 - void RedeemRewards(string bucket, int count, void* callback);
 - void UserCompletedAction(string action, string metadata);
 - void UserCompletedAction(string action);
 - void UserCompletedAction(string action, void* callback);
 - void UserCompletedAction(string action, string metadata, void* callback);
 - void CancelShareLinkDialog();
 - void GetCreditHistory(void* callback);
 - void GetCreditHistory(string afterID, int length, string order, void* callback);
 - void GetCreditHistory(string bucket, string afterID, int length, string order, void* callback);
 - int GetCredits();
 - void InitSession(string linkUrl = "", bool autoInitSession = false);
 - void InitSession(bool isReferrable, string linkUrl = "");
 - void InitSession(void* callback, string linkUrl = "");
 - void InitSession(bool isReferrable, void* callback, string linkUrl = "");

## Branch Content Metadata
ICOMBranchContentMetadataPtr comMetadata(__uuidof(COMBranchContentMetadata));

Branch Universal Object

ICOMBranchUniversalObjectPtr comBUO(__uuidof(COMBranchUniversalObject));

Branch Link Properties

ICOMBranchLinkPropertiesPtr comLinkProperties(__uuidof(COMBranchLinkProperties));

Example of use objects:

ICOMBranchContentMetadataPtr comMetadata(__uuidof(COMBranchContentMetadata));
comMetadata->AddCustomMetadata("testkey", "testvalue");

ICOMBranchUniversalObjectPtr comBUO(__uuidof(COMBranchUniversalObject));
comBUO->CanonicalIdentifier = "item/12345";
comBUO->CanonicalUrl = "";
comBUO->ContentIndexMode = "PRIVATE";
comBUO->LocalIndexMode = "PUBLIC";
comBUO->Title = "My Content Title";
comBUO->ContentDescription = "my_product_description1";
comBUO->ImageUrl = "https://example.com/mycontent-12345.png";
comBUO->AddKeyword("My_Keyword1");
comBUO->AddKeyword("My_Keyword2");

ICOMBranchLinkPropertiesPtr comLinkProperties(__uuidof(COMBranchLinkProperties));
comLinkProperties->AddTag("Tag1");
comLinkProperties->Channel = "Sharing_Channel_name";
comLinkProperties->Feature = "my_feature_name";
comLinkProperties->AddControlParam("$android_deeplink_path", "custom/path/*");
comLinkProperties->AddControlParam("$ios_url", "http://example.com/ios");
comLinkProperties->MatchDuration = 100;

COM Delegates

  • Init session delegate
void (ICOMBranchUniversalObject buo, ICOMBranchLinkProperties link, LPTSTR error);
  • Get Reward History delegate
void (string json, LPTSTR error);
  • Get Rewards delegate
void (bool changed, LPTSTR error);
  • Logout delegate
void (bool logoutSuccess, LPTSTR error);
  • Redeem Rewards delegate
void (bool changed, LPTSTR error);
  • Identity user delegate
void (string referrinParams, LPTSTR error);
  • User Completed Action delegate
void ();

Using of callback example

void __stdcall InitCallback(
    ICOMBranchUniversalObjectPtr comBUO,
    ICOMBranchLinkPropertiesPtr comLinkProperties,
    LPTSTR error) {
    //your code here
}

int main()
{
    HRESULT hr = CoInitialize(NULL);
    ICOMBranchPtr comBranch(__uuidof(COMBranch));
    comBranch->GetBranchInstance(false,"your branch key");
    comBranch->SetNetworkTimeout(3000);
    comBranch->InitSession_3(&InitCallback, "");
    CoUninitialize();
}

Updated 6 months ago


Windows C-Sharp Advanced Features


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.