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

GTM Custom Templates for Amplitude snippet #2

Open
racc-oo-n opened this issue Jul 1, 2019 · 1 comment
Open

GTM Custom Templates for Amplitude snippet #2

racc-oo-n opened this issue Jul 1, 2019 · 1 comment

Comments

@racc-oo-n
Copy link

Hello,
GTM recently released a new feature Custom Templates. It gives an ability to create templates for tags and variables in GTM, and the most useful feature – you can import and export those templates between projects.
I think it would be great to create GTM template for the Amplitude snippet and share it in this repository.
I've tried to move Amplitude snippet by myself, but GTM templates use sandboxed version of JavaScript, so I can't just copypaste the code. I found some troubles because I can't really get some parts of the snippet and don't know how you use it later.

Here's my code and my thouhgts about problem

This part of code works greate:

var setInWindow = require('setInWindow');
var injectScript = require('injectScript');
var callInWindow = require('callInWindow');
var logToConsole = require('logToConsole');

setInWindow('amplitude', {_q: [], _iq: {}});

injectScript('https://cdn.amplitude.com/libs/amplitude-4.4.0-min.gz.js', function () {
  if (!callInWindow('amplitude.runQueuedFunctions')) {
    logToConsole("[Amplitude] Error: could not load SDK");
  }
}, data.gtmOnFailure);

This part doesn't work:

var Identify = function() {this._q = []; return this;};
["add","append","clearAll","prepend","set","setOnce","unset"].map(function (method) {
  Identify.prototype[method] = function() {
    this._q.push([method].concat(Array.prototype.slice.call(arguments, 0))); return this;
  };
});
setInWindow('amplitude.Identify', Identify);

It throwns errors, such as

  • Illegal use of reserved word 'this' as an Identifier
  • Attempting to use undeclared variable Array in function

Because in sandboxed JS we can't

  • access window or document directly
  • pass non-plain objects
  • access DOM elements

I think we can rewrite this invalid code using method createArgumentsQueue or createQueue, but I really don't know how 🙂

Would be grateful for your help.

@beeremy
Copy link

beeremy commented Sep 28, 2020

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants