1

I'm trying to use the amazing features brought by the Custom Template on Google Tag Manager to organize the tags we use to log events on Amplitude.

I used to have a code like : amplitude.getInstance().logEvent(eventTitle, {args})

However, due to sandbox javascript we do not have direct access to amplitude.

So I tried to do:

const callInWindow = require('callInWindow');
const copyFromWindow = require('copyFromWindow');

const amplitude = copyFromWindow('amplitude');
callInWindow('amplitude.getInstance().logEvent', eventTitle, args);

And I gave full permissions on :

  • amplitude
  • amplitude.getInstance
  • amplitude.getInstance.logEvent

But the result is a tag failing with error saying : Tag XXX threw an error

The only workaround I found is to use a deprecated version of the API: amplitude.logEvent in the following way.

const callInWindow = require('callInWindow');
const copyFromWindow = require('copyFromWindow');

const amplitude = copyFromWindow('amplitude');
callInWindow('amplitude.logEvent', eventTitle, args);

And it works properly but I don't know for how long based on the deprecation announced by Amplitude : https://amplitude.github.io/Amplitude-JavaScript/Amplitude#amplitudelogevent

Does anyone know how I could in js sandbox first get the instance of amplitude.getInstance and then call used it's logEvent feature?

Your help would be highly appreciated.

Cheers!

Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

0

Does this code fix your issue?

const callInWindow = require('callInWindow');

const args = data.args.reduce((result, item) => {
  result[item.key] = item.value;
  return result;
}, {});

const amplitudeInstance = callInWindow('amplitude.getInstance');
amplitudeInstance.logEvent(data.eventTitle, args);

data.gtmOnSuccess();

You will need a text field called eventTitle and a table called args with key and value text columns for this code to work.

screenshot of fields

The template needed execute permissions for amplitude.getInstance, but that was all.

Ben Larson
  • 404
  • 1
  • 5
  • 11
  • Ben, thanks for your help. Unfortunately it doesn't work. Due to the fact that GTM uses sandbox js, what works in the console won't necessarily work in the template, and that's one case. Trying your suggestion I receive the same error message "Tag XXX threw an error". Any other idea? Thanks again. – Mombia Atiat Oct 06 '20 at 00:15
  • Hi Mombia, I'm sorry that didn't fix the error. I created an Amplitude account to test some stuff out and edited my answer above. Does that work for you? I was able to get the events firing to Amplitude with this new setup. – Ben Larson Oct 07 '20 at 01:48
  • @BenLarson Hey! I can't get ur code to work either. I get a `Can't read property logEvent of undefined.` error. Is there some other setup that you are doing? Do I need to change my amplitude-settings tag somehow? – Johan Hjalmarsson Nov 27 '20 at 10:14
  • Hi @JohanHjalmarsson. No, I don't think I had to edit the settings in Amplitude at all. I would just say make sure in the GTM Tag Template has execute permissions for `amplitude.getInstance`. Other than that I'd just log stuff out with the `logToConsole` [API](https://developers.google.com/tag-manager/templates/api#logtoconsole) to debug. Hope that helps! – Ben Larson Dec 01 '20 at 00:26