Google 태그 관리자를 사용하여 맞춤 템플릿을 만들었지 만 샌드 박스 자바 스크립트가 작동하도록하는 데 약간의 어려움이 있습니다. 다음과 같은 이유로 스크립트가 올바르게 삽입되었음을 알고 있습니다.
log ( 'Cotaylitcs : 스크립트가 성공적으로로드되었습니다.');
콘솔에 출력합니다. 내 함수를 호출 할 수 없습니다. JS 코드도 포함했습니다.
사용자 정의 템플릿 코드 (샌드 박스 Javascript)
// Enter your template code here.
const log = require('logToConsole');
const injectScript = require('injectScript');
const copyFromWindow = require('copyFromWindow');
const callInWindow = require('callInWindow');
const callLater = require('callLater');
const setInWindow = require('setInWindow');
const key = "testing";
const value = "test2";
const url = 'https://api.deve/cotalytics/cotalytics.js';
// If the script loaded successfully, log a message and signal success
const onSuccess = () => {
log('Cotaylitcs: Script loaded successfully.');
const cotalytics = copyFromWindow('cotalytics');
log(cotalytics);
callInWindow('cotalytics.addEvent',"testing12" ,{key: value}, "{{DL - cottageCode}}");
callInWindow('cotalytics.logEvents()');
data.gtmOnSuccess();
};
// If the script fails to load, log a message and signal failure
const onFailure = () => {
log('Cotaylitcs: Script load failed.');
data.gtmOnFailure();
};
injectScript(url, onSuccess, onFailure, 'cotalytics');
Sandboxed Js에 의해 주입되는 Javascript 코드 :
let Cotalytics = function(){
this.init();
};
// Init
Cotalytics.prototype =
{
init: function () {
var cotalytics = this;
// Argument Assignment
//cotalytics.endpoint = 'https://localhost:44301/api/events/',
//cotalytics.async = true,
//cotalytics.debug = true,
//cotalytics.events = {}
//creat cookie here
return cotalytics;
},
// Add Interaction Object Triggered By Events to Records Array
addEvent: function (eventType, data, cottageCode, brandid) {
data = data || {};
cottageCode = cottageCode || null;
data.Browser = window.navigator.appVersion;
var cotalytics = this;
// cotalytics Object
cotalytics.events = {
timestamp : new Date(),
cottagecode : cottageCode,
type : eventType,
ipaddress : "127.0.0.1",
requesturl : window.location.href,
sessionid : "1234567",
brandid : brandid,
eventData : Object.keys(data).map(function(key) { return {"key": key, "value": data[key]}})
};
// Log Interaction if Debugging
//cotalytics.logEvents();
//console.log("Session:\n", interactor.interaction);
//var data =JSON.stringify(interactor)
console.log("JSON:\n", JSON.stringify(cotalytics));
return cotalytics;
},
// Gather Additional Data and Send Interaction(s) to Server
logEvents: function () {
var cotalytics = this,
// Initialize Cross Header Request
xhr = new XMLHttpRequest();
// Post Session Data Serialized as JSON
xhr.open('POST', 'https://localhost:44301/api/events/', true);
xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
var data = JSON.stringify(cotalytics.events);
xhr.send(data);
return cotalytics;
}
};
window.cotalytics = new Cotalytics();
좋아, 대부분 거기에있는 것 같지만 옳지 않은 몇 가지가 있습니다.
{key: value}
addEvent 호출에서 사용 합니다. 값은 상수로 채워지지만 키는 채워지지 않습니다. 나는 이것이 동적 변수가 될 것이라고 상상하고 있는데, 왜 다음과 같은 것을 가지고 있지 않습니까?const kvp= {};
const key = "yourKey";
const value = "yourValue";
kvp[key] = value;
그러면 단순히 "키"가 아닌 원하는 키로 개체가 생성됩니다.
callInWindow('cotalytics.addEvent',"testing12" ,{key: value}, "{{DL - cottageCode}}");
dataLayer 변수처럼 보이지만 copyFromDataLayer API를 사용하여 시도해 보는 것은 어떻습니까?
const copyFromDataLayer = require('copyFromDataLayer');
const cottageCode = copyFromDataLayer('cottageCode');
callInWindow('cotalytics.logEvents()');
는 대괄호가 필요하지 않습니다 callInWindow('cotalytics.logEvents');
.내가 작업 한 최종 코드는 다음과 같습니다.
const log = require('logToConsole');
const injectScript = require('injectScript');
const callInWindow = require('callInWindow');
const makeTableMap = require('makeTableMap');
const copyFromDataLayer = require('copyFromDataLayer');
const cottageCode = copyFromDataLayer('cottageCode');
//Create the Key Value Pair
const kvp = {};
const key = "testing";
const value = "test2";
kvp[key]=value;
const url = "https://api.deve/cotalytics/cotalytics.js";
// If the script loaded successfully, log a message and signal success
const onSuccess = () => {
log('Cotaylitcs: Script loaded successfully.');
callInWindow('cotalytics.addEvent',"testing12" ,kvp,cottageCode);
callInWindow('cotalytics.logEvents');
data.gtmOnSuccess();
};
// If the script fails to load, log a message and signal failure
const onFailure = () => {
log('Cotaylitcs: Script load failed.');
data.gtmOnFailure();
};
injectScript(url, onSuccess, onFailure, 'cotalytics');
도움이 되었기를 바랍니다. 다른 질문이 있는지 물어보세요!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다