javascript - How can I access the Google Analytics dataLayer Variable in Aurelia? -


i'm using webpack typescript combo aurelia , want know how can push global datalayer variable provided google analytics in aurelia classes?

when try log get: cannot find name 'datalayer’.

but when log in console of browser, datalayer exists

any idea on how can expose it?

console.log(window.datalayer);  @inject(appstate, authservice, setupguide, endpoint.of('api'),  dialogservice, eventaggregator, router) export class smoke {} 

i know webpack can use shimming via:

new webpack.provideplugin({     $: "jquery",     jquery: "jquery",     "window.jquery": "jquery" }) 

is possible datalayer variable? need able update datalayer variable can setup enhanced ecommerce tracking.

update: i've tried:

(<any>window).datalayer = (<any>window).datalayer || []; 

does throw type error: "error: can't resolve 'datalayer'"

i need able access google analytics global variable , not sure why doesn’t work or how expose es6.

i'm using webpack , typescript , have added index.ejs follows:

<body aurelia-app="main"> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=gtm-okoko" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <script>    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':    new date().gettime(),event:'gtm.js'});var f=d.getelementsbytagname(s)[0],        j=d.createelement(s),dl=l!='datalayer'?'&l='+l:'';j.async=true;j.src=    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentnode.insertbefore(j,f);    })(window,document,'script','datalayer','gtm-ookok'); </script>  

update 2:

there no script tag loads app. aurelia binds body element. entire index.ejs

<!doctype html> <html>   <head>     <meta charset="utf-8">     <title><%- htmlwebpackplugin.options.metadata.title %></title>     <meta name="viewport" content="width=device-width, initial-scale=1">     <base href="/">     <link rel="apple-touch-icon" href="apple-touch-icon.png?v=2">     <link rel="icon" href="/favicon.png?v=5" />     <meta name="theme-color" content="#34274f">     <meta name="msapplication-navbutton-color" content="#34274f">     <meta name="apple-mobile-web-app-status-bar-style" content="#34274f">     <link rel="manifest" href="/manifest.json">       <% if (htmlwebpackplugin.options.metadata.env === 'local') { %>     <link rel='stylesheet' href='./src/main.css' type='text/css'>      <% } %>     <!-- imported css concatenated , added automatically -->   </head>   <body aurelia-app="main">     <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=gtm-ookok" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>     <script>        (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':        new date().gettime(),event:'gtm.js'});var f=d.getelementsbytagname(s)[0],        j=d.createelement(s),dl=l!='datalayer'?'&l='+l:'';j.async=false;j.src=        'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentnode.insertbefore(j,f);        })(window,document,'script','datalayer','gtm-ookok');     </script>   </body> </html> 

sample code uses variable:

import { router } 'aurelia-router'; import {appstate} '../../resources/global/appstate';  @inject(appstate, router) export class sample {    attached(){      console.log(datalayer);    } } 

adjust code follows. update main.ts file looks like

import {aurelia} 'aurelia-framework';  export function configure(aurelia: aurelia) {   // ... }  window.datalayer = window.datalayer || [];  declare global {   interface window {     // todo: replace more specific type based on usage     datalayer: any[];   } } 

note: aurelia specific code in snippet above exits provide context. answer answer not specific webpack rather applies typescript project using modules.

from here on access referencing window.datalayer. initialization code global declaration moved separate file , wrap in service acts proxy global if wish to.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -