在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):alinemorelli/react-gtm开源软件地址(OpenSource Url):https://github.com/alinemorelli/react-gtm开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):react-gtm-moduleReact Google Tag Manager ModuleThis is a Javascript module to React based apps that implement Google Tag Manager. It is designed to use GTM snippet. You can easily use custom dataLayer, multiple dataLayers and additional events. Installationnpm: npm install react-gtm-module --save UsageInitializing GTM Module: import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
gtmId: 'GTM-000000'
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app) DataLayerCustom dataLayer example:import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
gtmId: 'GTM-000000',
dataLayer: {
userId: '001',
userProject: 'project'
}
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app) Multiple dataLayer example:If you need send multiple custom dataLayer you can initialize GTM Module on different components sending different dataLayers You can initialize it normally: import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
gtmId: 'GTM-000000',
dataLayerName: 'PageDataLayer'
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app) And send your data in each page you want import React from 'react'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
dataLayer: {
userId: '001',
userProject: 'project',
page: 'home'
},
dataLayerName: 'PageDataLayer'
}
...
const Home = () => {
...
TagManager.dataLayer(tagManagerArgs)
...
return (
<div className='home'>
//your component code
</div>
)
}
export default Home EventsExample:import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
gtmId: 'GTM-000000',
events: {
sendUserInfo: 'userInfo'
}
}
TagManager.initialize(tagManagerArgs)
...
const app = document.getElementById('app')
ReactDOM.render(<Router routes={routes} />, app) EnvironmentsConfigure how Tag Manager will works between development and production server environments. Example:import React from 'react'
import ReactDOM from 'react-dom'
import Router from 'react-router'
import routes from './routes'
...
import TagManager from 'react-gtm-module'
const tagManagerArgs = {
gtmId: 'GTM-000000',
auth: '6sBOnZx1hqPcO01xPOytLK',
preview: 'env-2'
}
TagManager.initialize(tagManagerArgs) How can I find auth and preview?Go to Google Tag Manager -> ADMIN -> Environments -> Actions -> Get Snippet. Look for gtm_auth and gtm_preview Don't know to use GTM environments?
Note:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论