<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mumtaz1000</title>
    <description>The latest articles on DEV Community by mumtaz1000 (@mumtaz1000).</description>
    <link>https://dev.to/mumtaz1000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F539478%2F69bbb671-f3dd-4617-a09d-47b2b83edfa9.png</url>
      <title>DEV Community: mumtaz1000</title>
      <link>https://dev.to/mumtaz1000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mumtaz1000"/>
    <language>en</language>
    <item>
      <title>How to add react-gtm-module in  PWA Studio </title>
      <dc:creator>mumtaz1000</dc:creator>
      <pubDate>Thu, 23 Dec 2021 21:25:31 +0000</pubDate>
      <link>https://dev.to/mumtaz1000/how-to-add-react-gtm-module-in-pwa-studio-4ncm</link>
      <guid>https://dev.to/mumtaz1000/how-to-add-react-gtm-module-in-pwa-studio-4ncm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I have created this tutorial on Mac and have used yarn because it is recommended by PWA Studio. &lt;/p&gt;

&lt;p&gt;In this blog i will tell you how to add react-gtm-module in your PWA Studio storefront project. So there are some steps which need to be followed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step1:
&lt;/h2&gt;

&lt;p&gt;Your must have a storefront project in pwa studio. You can also create a new one by using command &lt;code&gt;yarn create @magento/pwa&lt;/code&gt; or &lt;code&gt;npm init @magento/pwa&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step2:
&lt;/h2&gt;

&lt;p&gt;You must install &lt;strong&gt;Fooman VeniaUiOverrideResolver for PWA Studio&lt;/strong&gt; either by using &lt;code&gt;yarn add @fooman/venia-ui-override-resolver&lt;/code&gt; or by using &lt;code&gt;npm i @fooman/venia-ui-override-resolver&lt;/code&gt;. The main reason behind installing Fooman extension is we need to override a component file where we will add our gtm module. For example if we want to add our gtm module in our main.js file then inside our src folder we have to create&lt;br&gt;&lt;br&gt;
&lt;code&gt;overrides/venia-ui/components/Main/main.js&lt;/code&gt; and inside main.js we have to copy and paste the whole code from &lt;br&gt;
&lt;code&gt;node_modules/@magento/venia-ui/lib/components/Main/main.js&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step3:
&lt;/h2&gt;

&lt;p&gt;Now we have created our main.js file inside &lt;code&gt;src/overrides/venia-ui/components/Main&lt;/code&gt; we have to create an index.js file inside &lt;code&gt;Main&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\index.js
export { default } from './main';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step4:
&lt;/h2&gt;

&lt;p&gt;Open your package.json file and inside your &lt;code&gt;"devDependencies"&lt;/code&gt; add "react-gtm-module" like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"react": "~17.0.1",
"react-dom": "~17.0.1",
"react-gtm-module": "~2.0.11",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open your terminal and type command &lt;code&gt;yarn install&lt;/code&gt; or &lt;code&gt;npm install&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step5:
&lt;/h2&gt;

&lt;p&gt;Now this is the main part where we have two options that either we add our react-gtm-module in our main.js directly or  create a new component inside &lt;code&gt;src/overrides/venia-ui/components/your-gtm-component-folder&lt;/code&gt; and then copy and paste the gtm-module code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Your gtm component.js
import React from "react"
import TagManager from 'react-gtm-module'

const tagManagerArgs = {
    gtmId: 'GTM-XXXXXXX'}

TagManager.initialize(tagManagerArgs)

const GoogleTagManager = () =&amp;gt; {

    window.dataLayer.push({

        event: 'MainPageView'

      });

return(
    &amp;lt;h1&amp;gt;Hello from GTM!&amp;lt;/h1&amp;gt;
)
}
export default GoogleTagManager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create index.js file inside your gtm component folder and  write&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\\index.js
export { default } from './your-gtm-component';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step6:
&lt;/h2&gt;

&lt;p&gt;Last but not least do not forget to import your gtm component inside your override main.js file and add it like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    return (
        &amp;lt;main className={rootClass}&amp;gt;
            &amp;lt;Header /&amp;gt;
            &amp;lt;YourGTMcomponent&amp;gt;
            &amp;lt;div className={pageClass}&amp;gt;{children}&amp;lt;/div&amp;gt;
            &amp;lt;Footer /&amp;gt;
        &amp;lt;/main&amp;gt;
    );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References and helpful links: &lt;br&gt;
&lt;a href="https://dev.toPwa%20Studio"&gt;https://developer.adobe.com/commerce/pwa-studio/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.toFooman%20overrider"&gt;https://github.com/fooman/venia-ui-override-resolver&lt;/a&gt;&lt;br&gt;
&lt;a href="https://dev.toreact-gtm-module"&gt;https://www.npmjs.com/package/react-gtm-module&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>gtm</category>
      <category>pwastudio</category>
      <category>magento</category>
    </item>
  </channel>
</rss>
