1

I am making a call to Google Analytics from salesforce. I am getting an error stating that the parameter 'v' is not being sent. This is included in the json body.

If I use a third party script like python to send the same parameters via an HTTP Post request, I am able to send them over. Yet, sending these parameters via Salesforce Apex is causing this error. Here is my python code that works.

import requests
from collections import OrderedDict
geoids='1000041'
referral='http://www.bing.com'
uid='GOOGLEUACODE'
payload = {"tid" : uid,   "v" : "1",   "cid" : "client_1",   "t" : "event",   "dp" : "www.bing.com",   "ds" : "web",   "dr" : "referral",   "geoid" : "1000041",   "ec" : "web",   "ea" : "web",   "el" : "web",   "ev" : "0"}
urls='https://www.google-analytics.com/debug/collect'
resp = requests.post(url=urls,params=payload,headers=user_agent)
print resp
print resp.url
print resp.status_code

If I attempt this exact same call in Apex, it fails with an error saying the paramter "v" has not been supplied. The exact error message is

 "description": "A value is required for parameter 'v'. Please see http://goo.gl/a8d4RP#v for details.",

Here is the Apex code for this.

string url= 'https://www.google-analytics.com/debug/collect';
string payload = '{"tid" : "GOOGLEUACODE",   "v" : "1",   "cid" : "client_1",   "t" : "event",   "dp" : "www.bing.com",   "ds" : "web",   "dr" : "referral",   "geoid" : "1000041",   "ec" : "web",   "ea" : "web",   "el" : "web",   "ev" : "0"}';
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(payload);
res = h.send(req);
System.Debug('response body '+res.getBody());

It looks like I am unable to send the body in the POST method. I have tried a few things like setting the Content-Type in the HTTP Request, but those have not worked.

What has worked is changing the method to a GET and converting the payload string into a different format like tid=GOOGLECODE&v=1&event=dp

So how can I preserve the format of the payload string in the original python code and do this?

If you want to try this code, you can use this UA-ID created by me for this purpose: uid='UA-78999084-1'

Edited: I am not trying to make an actual call in the code. The validation succeeds in the python code but not in the Apex code.

  • Is the User-Agent header needed? I noticed that it's not in your Apex Code, but is in your Python code. – sfdcfox Jun 9 '16 at 14:59
  • It is not needed. I will take it out from my python code. – abhi Jun 9 '16 at 15:56
0

it is different approach between Python "requests.post" and Salesforce "http.send"

python params

Python parameter that you are using, "params=payload" adds all the parameters to the URL request, so the output is:

https://www.google-analytics.com/debug/collect?tid=GOOGLEUACODE&v=1& ...

there is no native way to do so in Salesforce. You can write your own method that will take {"parameter":"value","parameter":"value"} string as input and write corresponding "parameter=value&parameter=value" output.

I managed to make POST also work, so it is not necessary to change method to GET.

string url= 'https://www.google-analytics.com/debug/collect'; 
string payload = 'v=1&cid=client_1&tid=UA-78999084-1&dp=www.bing.com&t=event&ds=web&dr=referral&geoid=1000041&ev=0';
Http h = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
req.setEndpoint(url);
req.setMethod('POST');
req.setBody(payload);
res = h.send(req);

System.Debug('response body '+res.getBody());
0

I am not sure if this is a correct answer. I guess the endpoint https://www.google-analytics.com/debug/collect is just to check or validate if its beeing hit. If you use https://www.google-analytics.com/collect instead, it should work

  • Yes. I am aware of that. Perhaps I need to edit my question. I am not trying to make an actual call. The validation succeeds in the python code but not in the Apex code. – abhi Jun 9 '16 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.