1

I'm working to use Google Optimize. I need to add this tag inside my page:

<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',4000,
{'GTM-X':true});</script>

to make this work in React, I'm trying the following:

import React from 'react';

const MarketingPage = class extends React.Component {
  compomentDidMount = () => {
    (function(a, s, y, n, c, h, i, d, e) {
      s.className+=' '+y;
      h.start=1*new Date;
      h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
      (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
      })(window,document.documentElement,'async-hide','dataLayer',4000,
      {'GTM-X':true}
    );
  }

React isn't liking this.. I'm getting the following error:

Move the invocation into the parens that contain the function

Is it possible to rewrite the code Google Optimize required to be React, linter friendly?

Thanks

2
  • 1
    I don't think it's React, but your linter. Just move (window,document.documentElement,'async-hide','dataLayer',4000,{'GTM-X':true}) inside the () surrounding the function instead. Commented Aug 3, 2018 at 21:13
  • 1
    @Tholle would you mind posting the answer? I tried that idea and it didn't seem to make a difference so perhaps I'm misunderstanding. thanks Commented Aug 3, 2018 at 21:14

1 Answer 1

3

I don't think it's React giving your this warning, but your linter.

You could put your function in a variable and invoke it right after instead.

const MarketingPage = class extends React.Component {
  compomentDidMount() {
    const analytics = function(a, s, y, n, c, h, i, d, e) {
      s.className+=' '+y;
      h.start=1*new Date;
      h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
      (a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
    };

    analytics(window,document.documentElement,'async-hide','dataLayer',4000,{'GTM-X':true});
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you -- that resolved the last issue now it created a new issue, it's saying unexpected unnamed function
@AnnaSm Alright. I updated the answer. Hopefully that will remove all linter warnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.