Skip to content

Instantly share code, notes, and snippets.

@Thorium
Last active March 16, 2022 22:09
Show Gist options
  • Save Thorium/40b733716b92b8d711fe to your computer and use it in GitHub Desktop.
Save Thorium/40b733716b92b8d711fe to your computer and use it in GitHub Desktop.
Google Analytics Typescript
/// <reference path="./../../paket-files/borisyankov/DefinitelyTyped/google.analytics/ga.d.ts" />
export var gaNewElem : any = {};
export var gaElems : any = {};
function gaInit(){
var currdate : any = new Date();
/* tslint:disable:no-string-literal */
/* tslint:disable:semicolon */
/* tslint:disable:no-unused-expression */
// This code is from Google, so let's not modify it too much, just add gaNewElem and gaElems:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*currdate;a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga', gaNewElem, gaElems);
/* tslint:enable:no-unused-expression */
/* tslint:enable:semicolon */
/* tslint:enable:no-string-literal */
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('send', 'pageview');
}
(function() {
gaInit();
});
@billysean
Copy link

on my screen, there's a problem of cannot find name 'ga'. can you help me why? thanks

@TheSecondPower
Copy link

In the tsconfig file, type sources can be specified in compilerOptions -> types.

{
   "compilerOptions": {
       "types" : ["google.analytics"]
   }
}

If I'm readying the docs correctly, TypeScript will also pull from the node_moduels/@types if types and typeRoots are not specified, but if they are this settings will need to be included.
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment