'Google App Script Add-On and Google Analytics Tracking

I am trying to track events in my google app script add-on (Working with Cards "runtimeVersion": "V8",), but unfortunately, the urls I am creating to post to GA are not making any impact on my google-analytics dashboard, checking the realtime monitor. But ... when I call the URLs in the browser address-bar, I am getting events tracked.

This is what my URL looks like:

https://ssl.google-analytics.com/collect?v=1&tid=*****&cid=APrZferAeSHwd9C/aYIQPlcTig1FoMOG6Q/9zD+W9EcG62NnYQgGpB0tfXd7TheppU1CwdObIjrr&t=event&aip=false&ds=google%20addOn&ec=trigger&ea=triggered&el=homepageTrigger&ev=1

And this is what I try to do in the GAS:

let options: object = {
    method: 'POST',
    payload: googleTrackEvent.toUrlParameterString(),
};

// @ts-ignore
console.log("https://ssl.google-analytics.com/collect" + "?" + options.payload);

let responseCode: any = UrlFetchApp.fetch('https://ssl.google-analytics.com/collect', options).getResponseCode();
if (responseCode < 200 || responseCode > 299){
    console.error({call: 'GATrack', error:responseCode});
} else {
    console.log(responseCode);
}

So the URL is taken from the console.log debug output under the ts-ignore line. As said, this url copied to a browser address-bar creates an event.

And the response from the URL is 200.

I also set the permission in the appscript.json

"oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",

What did I miss?



Solution 1:[1]

You are probably recognized as a bot / spam. Try to add other parameters to the call such as the host name (dh) and the pagePath (dp) or the location (dl) or even ua parameter: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ua

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michele Pisani