Last active
March 16, 2022 22:09
-
-
Save Thorium/40b733716b92b8d711fe to your computer and use it in GitHub Desktop.
Google Analytics Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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(); | |
}); |
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
on my screen, there's a problem of cannot find name 'ga'. can you help me why? thanks