[Help][Add-on][Google Analytics] event hits don't show up in Google Analytics, how to send events data from Add-on to Google Analytics?

89 views
Skip to first unread message

Victor Negîrneac

unread,
Mar 10, 2020, 6:04:13 PM3/10/20
to google-apps-sc...@googlegroups.com

Hello, everyone!

I am trying to incorporate some analytics in a spreadsheets add-on but I am not able to get the events to show up in Google Analytics, event though I am able to manually trigger the events by accessing the link corresponding to the event I want to send.

Let me start with what works.
If put the following in the browser and press enter the event gets registered in Google Analytics (GA):
Link: 
https://www.google-analytics.com/collect?v=1&t=event&tid=UA-160685285-1&cid=50454954-60b8-4d14-9d36-a96297abd634&ec=GATrackDemo&ea=Menu%20click%2019&el=updateThisSheet&ev=1&uid=4ffbf3a699177b1ed9e0c1e6dffc01b635e20e589b12a4ed7ab93ab164006b00
Result:

Screenshot 2020-03-10 at 21.59.38.png


Now what does not work.

In order to setup the analytics in Apps Script I have followed tips and ideas from Martin Hawksey and Romain Vialard.

When I use the add-on and try to send a hit to GA it never gets registered. I get no erros and I am able to get the 200 response code from google analytics server.


Here is the minimal code:


Code.js

[UPDATE] As noted by Martin, there was no cid in the code below, I have tested it before with the cid as well with not luck, added it below for reference

function onOpen(e) {
  var menu = SpreadsheetApp.getUi().createMenu('Menu');
  menu.addItem('Hit GA', 'hitGaTest').addToUi();
}

function onInstall(e){
  onOpen();

function hitGaTest(){
  var payload = 'v=1&t=event&tid=UA-160685285-1&cid=50454954-60b8-4d14-9d36-a96297abd634&ec=GATrackDemo&ea=Menu%20click%2019&el=updateThisSheet&ev=1&uid=4ffbf3a699177b1ed9e0c1e6dffc01b635e20e589b12a4ed7ab93ab164006b00';
  var options = {'method' : 'POST',
                 'payload' : payload};
  var rep_code = UrlFetchApp.fetch('https://www.google-analytics.com/collect', options).getResponseCode();

  // Same issue with this `batch`
  // var rep_code = UrlFetchApp.fetch('https://www.google-analytics.com/batch', options).getResponseCode();
  console.log(rep_code); // 200
}

appscript.json

  "timeZone": "Europe/Lisbon",
  "dependencies": {
    "enabledAdvancedServices": [{
      "userSymbol": "Calendar",
      "serviceId": "calendar",
      "version": "v3"
    }]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "executionApi": {"access": "ANYONE"}
}


Is anyone here using GA in their add-ons that could check that it is still working?


Things I have tried and didn't work:

- Different browsers for running the script

- Publishing the add-on in G Suite Marketplace (as unlisted)

- Running the add-on from another developer account (different person in another country)

- Sending the request to either https://www.google-analytics.com/collect or https://www.google-analytics.com/batch end points

- Using Martin's GATrack code


Please help debugging this

Kind regards,

Victor


PS Wish you all a coding life free of bugs :)

Romain Vialard

unread,
Mar 13, 2020, 4:55:59 AM3/13/20
to Google Apps Script Community
Yes, still working for me.
You can try this library, see if it's working better (it's the one I'm using):

Martin Hawksey

unread,
Mar 14, 2020, 3:17:18 AM3/14/20
to Google Apps Script Community
Looking at your payload it appears you are missing a cid parameter which according to the GA hit builder is required https://ga-dev-tools.appspot.com/hit-builder/

To help with debugging you can also hit a validation URL https://www.google-analytics.com/debug/collect?tid=fake&v=1

More details in https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits

Hope that helps
Martin

Victor Negîrneac

unread,
Mar 14, 2020, 2:16:33 PM3/14/20
to Google Apps Script Community
Hi Romain!

Thank you for the reply.

I added your library to my project but still can't get the hits to show up.

Below I will post what else I have tried. Any ideas for what else I could try are very welcome

Victor Negîrneac

unread,
Mar 14, 2020, 2:20:39 PM3/14/20
to google-apps-sc...@googlegroups.com
Here are some extra things I have tried after reading the replies to my post, however without success:

- Creating new apps script project
- Creating a new Google Analytics account and getting a new tracking code (testing in my old and new apps script project)

Could it be that for recent GA accounts there is an underlying filter for hits that come from the Google servers? (I don't have any user defined filters)

Any suggestions are welcome

Kind regards

[EDIT] PS if you know of any GA alternative service that also allow for direct hits from apps script please let me know

Victor Negîrneac

unread,
Mar 14, 2020, 2:38:42 PM3/14/20
to Google Apps Script Community
Hello Martin!

Thank you for your reply and resources!

Regarding the cid parameter, I have tried both cases using it and not using it, no luck in either case.
Furthermore, the hit builder recognises a valid hit without the cid if the uid is provided:

Screenshot 2020-03-14 at 18.11.10.png


BUT it is true that I am not able to see the hits in the real time view of GA when I do not provide the cid (and send the hit through the button above or direct link access in the browser...). This behaviour is strange, indeed, because it is documented that the cid should not be necessary when the uid is provided...

I have also checked the debug end point and I do get a response from the server with a positive recognition of the hit:

Screenshot 2020-03-14 at 18.21.49.png

But still can't see the events in GA :(

Any other suggestions are very welcome

Kind regards

Martin Hawksey

unread,
Mar 15, 2020, 3:39:11 AM3/15/20
to google-apps-sc...@googlegroups.com
When you validate the hit have you tried clicking the 'Send hit to Google Analytics' button and seeing if it shows in your GA realtime data?



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0d55b541-4d2a-4984-a919-55f8a5094ff4%40googlegroups.com.
Screenshot 2020-03-14 at 18.11.10.png
Screenshot 2020-03-14 at 18.21.49.png

Victor Negîrneac

unread,
Mar 15, 2020, 9:16:55 AM3/15/20
to google-apps-sc...@googlegroups.com
Yes, it does show up in the GA's real time view when I use that button (I have to include the cid though)

Martin Hawksey

unread,
Mar 15, 2020, 9:22:47 AM3/15/20
to google-apps-sc...@googlegroups.com
I'm guessing the missing cid is the issue in your coffee. You can generate an id in apps script with Session.getTemporaryActiveUserKey() https://developers.google.com/apps-script/reference/base/session#getTemporaryActiveUserKey()

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/rtdzKUOEDZ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/7908f3d3-5b90-4ca4-9506-aa2552feb47c%40googlegroups.com.

Victor Negîrneac

unread,
Mar 15, 2020, 4:58:17 PM3/15/20
to Google Apps Script Community
Thank you for your reply, I believe I am doing what you suggested, but still not getting it to work :( 
Here is a short video that shows all the steps:


Please let me know if you spot something wrong with my code

Martin Hawksey

unread,
Mar 16, 2020, 3:46:00 PM3/16/20
to Google Apps Script Community
I tried your code with one of my own web properties and it worked no problems. I don't think this is an issue with your code but with how your Google Analytics account is setup. Unfortunately, I don't know enough about GA setup to suggest which setting is blocking the hit but in the admin for your property it would be worth check what filters have been setup

Victor Negîrneac

unread,
Mar 16, 2020, 4:58:18 PM3/16/20
to Google Apps Script Community
Thank you so much!! I will investigate more the GA admin page. Will post here if i find a solution

Kind regards
Reply all
Reply to author
Forward
0 new messages