Setup

LAST EDIT Jan 26 2021

The code below shows how to install Stream's Analytics SDK:

1
2
3
// the client is available via CocoaPods, just add this to your Podfile 
 
pod 'stream-analytics-ios'
1
// download the latest release from here: https://github.com/GetStream/stream-analytics-android/releases/
1
2
3
4
5
// Add this async loader code anywhere in your HTML code 
 
<script type="text/javascript"> 
  !function(t,e){t("StreamAnalytics", "https://d2j1fszo1axgmp.cloudfront.net/2.7.0/stream-analytics.min.js", e)}(function(t,e,n){var s,i,r;n["_"+t]={},n[t]=function(e){n["_" + t].clients = n["_" + t].clients || {}, n["_" + t].clients[e.apiKey] = this, this._config = e};var c=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(arguments),this}};s=["setUser","trackImpression","trackEngagement"];for(var a=0;a<s.length; a++){var o=s[a];n[t].prototype[o]=c(o)}i=document.createElement("script"),i.async=!0,i.src=e,r=document.getElementsByTagName("script")[0],r.parentNode.insertBefore(i,r)},this); 
</script>

Include the above code snippet in the <head></head> section of your page.

JavaScript is loaded asynchronously for optimal performance.

Using CommonJS Modules

Copied!

If you're using CommonJS modules, run the following command in your project directory:

1
npm install stream-analytics --save

After installing the package, require it in your app:

1
const StreamAnalytics = require('stream-analytics');

Client Setup

Copied!

The snippet below shows you how to initialize Stream's analytics client:

1
2
3
4
const client = new StreamAnalytics({ 
  apiKey: "YOUR_API_KEY", 
  token: "ANALYTICS_TOKEN" 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Initialize StreamAnalytics in AppDelegate 
 
#import "AppDelegate.h" 
#import "Stream.h" 
 
@interface AppDelegate () 
@end 
 
@implementation AppDelegate 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    //enable debug logging 
    [StreamAnalytics enableLogging:YES]; 
    return YES; 
} 
@end
1
2
3
4
5
6
7
8
9
// Import the library in your Gradle project: 
 
dependencies { 
  compile files('/path_to_file/stream-analytics-release-1.1.0.aar') 
} 
 
// Initialize the StreamAnalytics client 
StreamAnalyticsAuth auth = new StreamAnalyticsAuth("YOUR_API_KEY","ANALYTICS_TOKEN"); 
StreamAnalytics client = StreamAnalyticsImpl.getInstance(auth);

Bind the service in your application's manifest.xml:

1
<uses-permission android:name="android.permission.INTERNET" />

Always specify the current user before sending events:

1
2
// user id and a friendly alias for easier to read reports 
client.setUser({ id: 486892, alias: 'Julian' });
1
2
3
4
5
6
7
8
#import "Stream.h" 
 
 
// set user id only  
[[StreamAnalytics sharedInstance] setUserId:@"486892"]; 
 
// add friendly alias for the user 
[[StreamAnalytics sharedInstance] setUserId:@"486892" andAlias:@"Julian"];
1
2
3
4
5
// set user data 
client.setUser("486892", "Eric"); 
 
// you can also set the userId only 
// client.setUserId("486892");