matomo-org / matomo-sdk-ios

Matomo iOS, tvOS and macOS SDK: a Matomo tracker written in Swift
MIT License
390 stars 164 forks source link

OS unknown, visitors not counted in real time API #75

Closed orenk86 closed 7 years ago

orenk86 commented 9 years ago

Hello, I've been using this SDK in my app for a few months now, it's pretty awesome! Keep up the good work!!

Lately I've been experiencing a weird problem, visitors from my iOS app get registered with "unknown" under the OS and browser fields (other parameters and actions get registered properly), and when I use the real time API to get the number of visitors who performed a certain action in the app in the last few minutes, visitors from the iOS app aren't counted.

This doesn't happen with visitors on the website, and I have a feeling that the 2 problems are related to each other...

Here's some of my code. I init the tracker after the app gets the Piwik server's base URL and side ID from a configuration sent from the REST API, and not immediately when the app launches:

[PiwikTracker sharedInstanceWithSiteID:config.piwikSiteId baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/piwik/", config.piwikBaseUrl]]];
[PiwikTracker sharedInstance].debug = NO;
[PiwikTracker sharedInstance].dispatchInterval = 1;
[PiwikTracker sharedInstance].eventsPerRequest = 1;
[[PiwikTracker sharedInstance] setUserID:apiManager.loggedInUser.id];
[PiwikTracker sharedInstance].sessionStart = YES;

Then, I send views like this:

[[PiwikTracker sharedInstance] sendView:@"foo"];

And events like this:

[[PiwikTracker sharedInstance] sendEventWithCategory:@"foo" action:@"bar" name:@"bla-bla-bla" value:@(1)];

The segment I send to the real time API is:

'eventAction==bar;eventName=@bla-bla-bla'

This returns the correct numbers, but only for the visitors on the website, without the ones from the app. Am I missing something here? are there any extra parameters I need to send out to the Piwik server? In full disclosure, we have moved our Piwik server to a new machine a few weeks ago. Perhaps we need to have some module/plugin installed on the server? Or should I configure the app's tracker differently?

Thanks!

mattiaslevin commented 9 years ago

Your tracker configuration looks correct, not much that can go wrong here.

What exactly is being started as "unknown"? - the OS, provider, country etc (from your description I will assume its the OS)

I am not an expert of the internal workings on the Piwik server but the OS version is probably derived form the User-Agent header. The user Agent-Header is set by what ever networking framework you are using (either iOS default NSURLSession of AFNetworking).

Can you please print the headers of there request just before it is sent? E.g. in PiwikNSURLSessionDispatcher

I have seen "unknown" for Provider and Country from time to time when running in the simulator. I solved this by setting an explicit language in the simulator.

There a lots of different config option in the Piwik Server. I suggest you ask one of the backend guys for help as well.

Lets start with printing the User-Agent.

mattiaslevin commented 9 years ago

If you sign in to the Piwik server and select Administration -> Divide detection you can paste the User-Agent profile and see how it is interpreted by the server.

screen shot 2015-07-04 at 14 13 08
orenk86 commented 9 years ago

Okay, turns out the User-Agent is nil... Should I set it explicitly? And if so, to what value? Is there a template I need to use?

The browser field is always unknown (I think this is why the server doesn't count this as a visitor, although the number of visitors does increment when I use the app), and the OS field is sometimes unknown and sometimes Mac OSX. (Perhaps it's set to OSX after I recently used the same user to log in to the website?)...

Regarding the requests from the app, here are the headers:

URL - http://piwik.our-piwik-server.com/piwik/piwik.php?<query params, written below>
Status - Complete
Response Code - 204 No Response
Protocol - HTTP/1.1
Method - GET
Kept Alive - Yes
Content-Type - text/html
Client Address - /<device-ip>
Remote Address - piwik.our-piwik-server.com/<server-ip>

And the query params:

idsite - 2
rec - 1
apiv    1
url - http://<app-name>/screen/one/two/three/bla-bla-bla
_idvc - 39
r - 34546
s - 0
_viewts - 1436044053
_id - 17eed06d1b1032a6
_idts - 1435753380
e_c - foo
h - 0
_cvar - {"1":["Platform","iPod Touch 5G"],"2":["OS version","8.3"],"3":["App version","5"]}
e_v - 1
res - 640x1136
uid - <my-user-id>
send_image - 0
e_n - bla-bla-bla
m - 8
e_a - bar
cdt 2015-07-04 21:08:00
orenk86 commented 9 years ago

Alright, now I get the user agent string like this:

UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webview stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];

And set it manually to the PiwikTracker (btw, if this is an actual bug in the SDK, I'll be more than happy to update your code and submit a pull request). The device detection section now looks like this when I paste the user agent string into it:

screenshot 2015-07-05 10 27 25

The string from my testing device is:

Mozilla/5.0 (iPod touch; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F69kalturaNativeCordovaPlayer

But sadly, viewers from the app are still not counted in the real time API...

mattiaslevin commented 9 years ago

There should be no need to set the User-Agent header unless you want to set it to a specific value (which you normally do not). If it is nil, it is a bug and I very much would like to have additional information.

I have a couple of questions:

  1. Are you using the AFNetworking or NSURLSessionDispatcher
  2. Can you make sure you are actually printing the headers of the request (not the response, see below)

The http request details above actually looks more like the http response. Make sure you run the following debug command to get info about the request. You can see what I am getting below.

(lldb) po task.originalRequest.allHTTPHeaderFields
{
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "PiwikTrackeriOSDemo/1.0 (iPhone Simulator; iOS 8.3; Scale/2.00)";
}
orenk86 commented 9 years ago

Apologies, I was copying from the wrong side of the sniffer... :) I'm using NSURLSessionDispatcher. Here are the request headers:

{
    "User-Agent" = "Mozilla/5.0 (iPod touch; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12F69kalturaNativeCordovaPlayer";
}

Looks like I'm missing a few...

mattab commented 8 years ago

@orenk86 is there a bug in iOS SDK (what exactly would be the issue?) or could this ticket be closed?

orenk86 commented 8 years ago

Hi @mattab , Sadly, since I have opened this issue we were forced to abandon the Piwik SDK due to the need for tvOS support (the Piwik SDK was integrated as part of a bigger library we use in all of our projects). So feel free to close this ticket if you want, but keep in mind that there might still be a bug.

Last time I checked, the issue still exists. I can see the iOS users in the dashboard, but they aren't counted in the real time API.

If you plan to investigate further and there's anything I can do to help out - please let me know and I'll do my best.

brototyp commented 7 years ago

Hi, thank you for your Pull Request. We dropped ongoing development for version 3 of this SDK. Please consider looking into version 4, which is completely rewritten in Swift and supports tvOS.