Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setClientId() not specified receives a null value #1

Open
yanict opened this issue Apr 18, 2016 · 6 comments
Open

setClientId() not specified receives a null value #1

yanict opened this issue Apr 18, 2016 · 6 comments

Comments

@yanict
Copy link

yanict commented Apr 18, 2016

Hi,

I noticed I had a lot of log entries with the following message :
Uncaught PHP Exception InvalidArgumentException: "For Analytics object, you must specify a value to be set for setClientId"

Also, after looking for a while in my code, I found that there was an error in the AnalyticsFactory class on line 25.
->setClientId($request->cookies->get('_ga', null))

Based on the Google Analytics Measurement Protocol library for PHP the **setClientId _method need a value and cannot receive a _null.

Am I right and does anybody also had this problem ?
Thanks for your advice.

@derpue
Copy link

derpue commented Apr 26, 2016

To my information the client ID is also not the whole _ga cookie, instead its somthing like explode(".", $_COOKIE['_ga'])[2]

@yanict
Copy link
Author

yanict commented Apr 29, 2016

indeed you were right... almost ;)
I found this : https://plus.google.com/u/1/110147996971766876369/posts/Mz1ksPoBGHx

basically we need to pass the value after the second dot.
for example, if the _ga value is
1.2.299259584.1357814039257
you will have to do
->setClientId(299259584.1357814039257)

PHP code is in the link I pasted if you need.

@yanict yanict closed this as completed Apr 29, 2016
@foaly-nr1
Copy link
Contributor

Any chance one of you can open a PR? @derpue @yanict

@foaly-nr1 foaly-nr1 reopened this May 5, 2016
@derpue
Copy link

derpue commented May 5, 2016

@yanict Yes you are right, both the random ID (3rd part) and the timestamp the cookie was created (4th part) are together the clientId. Just did a test comparing the cookie and the value returned from the GA Tracker object in Javascript.

@ALL How should the exact behavior be defined?
I mean, this library is normally used to track server side events (eg a REST Interface) and not a normal user browsing the page (as this is all done by the normal GA client-side Javascript), thus we normally don't have a _ga Cookie, most of the time also we will set our own cookie, if we don't have a way to identify the user by eg login credentials.

Should this bundle handle all automagically? Test for _ga cookie, if not set, create own clientId, set own cookie and so on (needs also all be configurable).
Or do it as it's now and use the _ga cookies clientId if present just for convenience, but do a more explicit documentation of that clientId issue?

As the clientId is mandatory, but can't be set in parameters as the other mandatory values, make it more explicitly clear by changing the factory to somewhat $analytics = $container->get('gamp.factory')->create($clientId)

Dunno whats the best way

@foaly-nr1
Copy link
Contributor

@derpue Leaving it at @Nightbr's implementation for now.

@Nightbr
Copy link
Contributor

Nightbr commented Jun 8, 2016

I'm not reopening this issue but here we are with this:

  • My solution which retrieves automatically the _ga cookie works fine with a classic website which set the cookie...
  • But if you have a REST API (also my case), there is no _ga cookie and it throws an error beacause I haven't the clientId parameters...

For now, I workaround this by setting the clientId equals to the userId $this->analytics->setClientId($user->getId());

For example, the service Segment do the same thing:

If you do not pass this through, we look for the userId or anonymousId value and set it as the cid.

https://segment.com/docs/integrations/google-analytics/

But it is not ideal... you need to send event only if user is in context (you can send event with anonymous user without the _ga cookie)...

One solution can be to generate the cookie and set it into the header when the clientId is null. But it will work for application which handles cookies and read the header set-cookie... And if the client application of our REST service doesn't manage cookie, this solution will generate one clientId to each request and it will generate a new user each time...

I continue thinking about this... but one solution can be to stop reporting Analytics through the backend and use only JavaScript to report data... Develop webapp and no more website...

@foaly-nr1 foaly-nr1 reopened this Jun 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants