Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

How to load Google Analytics plugins GTM server side

need some help!

I would like to know if it is possible to load GA plugins scripts through my server in GTM, I manage to load analyticis.js but when I try to do the same with the plugins I doesn't work, I was wondering if it is possible to do that.

Load analytics.js was easy since there is an api provide by google:

// Request Analytics.js
if(getRequestPath() === '/analytics.js'){ 
  claimRequest();
  
  getGoogleScript('ANALYTICS', (script, metadata) =>{
    for (let header in metadata){
    setResponseHeader(header, metadata[header]);
    }
    setResponseHeader('content-type', 'text/javascript');
    setResponseBody(script);
    
    const origin = getRequestHeader('Origin');
    if (origin){
    setResponseHeader('Access-Control-Allow-Origin', origin);
    setResponseHeader('Access-Control-Allow-Credentials', 'true');
    }
    returnResponse();
  });
}

This API only accepts ANALYTICS, GTM and GTAG as scripts but I need google-analytics.com/plugins/ua/linkid.js and google-analytics.com/plugins/ua/ec.js loading server side too. is that possible?

I manage to write this code:

if(getRequestPath() === '/linkid.js'){ 
  claimRequest();
  
  sendHttpGet('https://www.google-analytics.com/plugins/ua/linkid.js', (statusCode, headers, body) => {
  setResponseBody(body);
  setResponseStatus(statusCode);
    log('Body: ' + body);
    log('Status: ' + statusCode);
    log('Headers: ' + headers);
  returnResponse();
});      
}

But It doesn't work properly when I go to my website the scripts are loaded from google: enter image description here

Any help is much appreciated and welcome! Kind regards!

question from:https://stackoverflow.com/questions/65932962/how-to-load-google-analytics-plugins-gtm-server-side

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...