SPFx-Google-Analytics icon indicating copy to clipboard operation
SPFx-Google-Analytics copied to clipboard

How can we replace Eval form the analytics?

Open lovethakker02 opened this issue 4 years ago • 0 comments

Hi. I am using this solution. But We found out that Eval is not best practice. So we have tried below code for replacing Eval :

var gtmScript = document.createElement("script");
 gtmScript.type = "text/javascript";
 gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${trackingID}`;
 gtmScript.async = true;
 document.head.appendChild(gtmScript);

 (window as any).dataLayer = (window as any).dataLayer || [];
 (window as any).dataLayer.push(datalayerval);
 (window as any).dataLayer.push({
   'event': 'page-variables-NewDetails',
   'sharepoint-user-id': userID,
   'sharepoint-user-location': location,
   'sharepoint-user-division': division,
   'page-type': pageLayoutType,
   'article-name': articleName,
   'language': language,
   'page-published': firstPublishedDate,
   'web-title': this.context.pageContext.web.title,
   'sharepoint-user-email': Email,
   'sharepoint-user-first-name': firstName,
   'sharepoint-user-last-name': LastName,
   'sharepoint-user-mobile': mobile,
 });
 (window as any).dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });

It is showing output in console. But the analytics is not able to catch it and the page count goes down when we deploy this. Can anyone tell how can we get rid of Eval?

lovethakker02 avatar Apr 21 '20 14:04 lovethakker02